面试官问我:定义静态在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