我只是想澄清一下。有什么区别-
private static int STUDENT_AGE = 18;
和
private static final int STUDENT_AGE = 18;
场内?
Jon Skeet 解释为“根本与特定实例无关”,好吧,我想我明白了。那么 final 在这种情况下到底做了什么?
下面的代码不起作用是因为学生年龄被分配为静态最终?这是否意味着根本无法覆盖默认年龄?那么是否可以创建指定除默认年龄以外的年龄的构造函数?
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;
}