方法2:用initialize block (初始化區塊)
方法3:用final method
public class InitializeDemo {
public static void main(String [] args){
Student s = new Student();
System.out.println(s.id);
}
}
class Student{
int id;
//Method 1:
public Student(int id){
this.id = id;
}
//Method 2:
{
id =1;
}
//Method 3:
int phone = initializePhone();
protected int initializePhone(){
return 3;
}
public Student(){
}
}
參考自Reference