Hi,I needed to know why we should use modifiers for variables?
I have this piece of code:
import java.util.*; public class Test{ public String student; private int sGrade; public void shame(String stdn){ student=stdn; } public void setGrade(int grade){ sGrade=grade; } public void printInfo(){ System.out.println("Student's name is " + student); System.out.println("Grade = " + sGrade); } public static void main(String args[]){ Test dars = new Test(); dars.shame("james"); dars.setGrade(17); dars.printInfo(); Test dars2 = new Test(); dars2.shame("jim"); dars2.setGrade(17); dars2.printInfo(); } }
now, if I use < String student > and <int sGrade> instead of public String student and private int grade as instance variables in the code above, I'll get the same result!
what good are they for?!
thanks
--- Update ---
Another one
in this code:
public class Reverse{ public String name; private int grade; public void student(String s,int g){ s=name; g= grade; } public void print(){ System.out.println("name is "+name); System.out.println("grade is "+grade); } }
when I invoke the method student("Jim", 15), it would give me null and 0 for name and grade, BUT, if I change the code to name = s and grade = g, it will give me Jim and 15....why is that?