-1

An interviewer asked me this:

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;    
    }
}

What happens if define below

private static Employee emp;

What is advantage with define static and non-static non access modifer with employee object?

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
user1357722
  • 7,088
  • 13
  • 34
  • 43
  • 1
    possible duplicate of [What is the exact meaning of static fields in Java?](http://stackoverflow.com/questions/797964/what-is-the-exact-meaning-of-static-fields-in-java) – Thilo Apr 26 '12 at 04:23

3 Answers3

2

If a field is defined static, then the value of that field is shared by all the instances of a that particular class. In your case how ever, that field is defined as private, which restricts instances of the class to access it outside the class. When your fill dataget() called it will create the list of Employee and static emp field will hold the value of the last emp("Prasad"). If any other instance of class EmpDetails is created and you try to access emp without calling fillData, using some other method e.g. GetEmp(), then it will return the last value for emp which was set to "Prasad".
With respect to design, this approach is not correct, as EmpDetails class will be pointing to one Employee due to static Employee object.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • I am not sure but can it be helpful for memory utilizing? If multiple objects of `Employee` then it will occupy multiple memory location. In this case it will occupy single memory location... Am I right or wrong? – Ketan Bhavsar Apr 26 '12 at 04:36
  • when i am iterating the list with emp object.I got suresh and prasad values Ouput is Size=======2 Size=======suresh Size=======Prasad – user1357722 Apr 26 '12 at 04:38
  • @user1357722, yes you will get two values for the list, but try getting the emp field value , that will return Prasad only, not suresh, and it will be for all instances of EmployeeDetails class – Habib Apr 26 '12 at 04:48
0

If you define a static Employee member field in theEmpDetails class - this means that all EmpDetails instances share the same Employee instance. The rest of your question doesn't really make much sense without more context.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

You should consider to read up on the difference between instance variables and class variables, for example by reading the Oracle's tutorial on the topic.

matsev
  • 32,104
  • 16
  • 121
  • 156