面試官問我:定義靜態在Java
class Employee{
private string empname;
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
}
class EmpDetails{
private static Employee emp;
public static List fillData(){
emp=new Employee();
List l=new ArrayList();
System.out.println("static after new creation fillData"+System.identityHashCode(emp));
emp.setEmpname("suresh");
emp.setDesignation("Sr.Software Engineer");
l.add(emp);
emp=new Employee();
System.identityHashCode(emp);
System.out.println("static after new creation fillData"+System.identityHashCode(emp));
emp.setEmpname("Prasad");
emp.setDesignation("Software Engineer");
l.add(emp);
return l;
}
}
如果定義如下
private static Employee emp;
會發生什麼有什麼優勢與定義與員工的靜態和非靜態的非接入改性劑目的?
可能重複[在Java中靜態字段的確切含義是什麼?](http://stackoverflow.com/questions/797964/what-is-the-exact-meaning-of-static-fields-in- java) – Thilo 2012-04-26 04:23:20