I just would like to clarify this. What is the difference between –
private static int STUDENT_AGE = 18;
and
private static final int STUDENT_AGE = 18;
within the field?
Jon Skeet explained as “not related to a particular instance at all”, ok I think I understand it. Then what does final do in this case exactly?
Below code does not work is it because student age is assigned as static final? Does it mean the default age can not be overwritten at all? then is it possible to create the constructor that specify an age other than the default?
private String myName;
private String myAddress;
private int myYearEnrolled;
private String myGender;
private static final int STUDENT_AGE = 18;
public Student(String name, String address, int year, String gender) {
myName = name;
myAddress = address;
myYearEnrolled = year;
myGender = gender;
}
public Student(int age)
{
STUDENT_AGE = age;
}
Once a variable is declared
final, its value cannot be changed later. In the code sample you provided, a constant is declared for defining the age of a student for a particular activity. It might mean that there will be a condition where for certain activity, the age of the student will be compared with this constant. If the age of student is greater than 18, then only he will be allowed to proceed or not.