Passing object reference to a method [duplicate]

ぐ巨炮叔叔 提交于 2019-12-11 03:28:37

问题


If the Employee reference is made null in the changedetails(), variable id value is retained and NullPointerException is not thrown (Code 1) may be because we just pass a copy of object's reference, but in Code 2 why the variables value has changed

Code 1:

public class JavaPassing {
    public static void changedetails(Employee e)
    {
        e=null;
    }

    public static void main(String args[])
    {
        Employee emp = new Employee("Vishal",7);
        changedetails(emp);
        System.out.println(emp.id);
    }
}

Code 2:

public class JavaPassing {
    public static void changedetails(Employee e)
    {
        e.id=9;
    }

    public static void main(String args[])
    {
        Employee emp = new Employee("Vishal",7);
        changedetails(emp);
        System.out.println(emp.id);
    }
}

回答1:


      -------- 
A -->| Object |<-- B
      --------

A.id = 10; // Property of object modified
B.id = 10; // Property of object modified here also

B = null ; // B is set to null
      -------- 
A -->| Object |     B (reference is null)
      --------

Here when you set B to null, A is not modified it will continue to point the Object in heap.

And that is why it will not throw NullPointerException if you will access id from reference A. All you are confused is between reference of Object and Object in memory.

In your case, A is emp and B is e.

You can find some good explanation in this question.




回答2:


In both cases Reference 'e' in changedetails() and 'emp' in main() both point to same object.

In code (1)

In changedetails() when you make e=null; ONLY e STOPS POINTING TO OBJECT. BUT emp CONTINUES TO POINT TO OBJECT. So in main() when you do emp.id value prints and no NullPointerException

In code (2) In changedetails() when you make e.id=9 remember Both References are pointing to same object i.e.
'e' in changedetails() and 'emp' in main() point to same object .... So e.id=9 means change made on same object hence when you do emp.id in main() value is 9




回答3:


  1. In java references to objects are passed by value.

So,

public static void main(String args[])
{
    Employee emp = new Employee("Vishal",7);
    changedetails(emp);  / /object Employee ahs only one reference - "emp"
    System.out.println(emp.id);
}


public void   changedetails(Employee emp1){ // here both emp1 and emp of main() point to the same Employee object.
emp1.setId(100); // now since emp1 also points to same Employee object, the data will be changed.
emp1 = null;// now emp1 points to null. So, only emp is pointing to E,ployee

}



回答4:


The argument you pass into the method changedetails() is a variable of its own, different from the variable emp in your main() method. They both refer to the same eployee so. Hence if you refer to the employee and change its state, the change is visible in both methods, changedetails() and main(). But if you assign null to the argument variable of the method changedetails() this is a local change visible only in that method.

Sidenote: it is considered bad practice to change the value of method arguments. And after leaving the method changedetails(), the method arguments are gone, because they live on the stack and not on the heap.



来源:https://stackoverflow.com/questions/22605760/passing-object-reference-to-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!