This question already has an answer here:
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);
}
}
So,
Here when you set
B
tonull
,A
is not modified it will continue to point theObject
in heap.And that is why it will not throw
NullPointerException
if you will accessid
from referenceA
. All you are confused is between reference of Object and Object in memory.In your case,
A
isemp
andB
ise
.You can find some good explanation in this question.
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
The argument you pass into the method
changedetails()
is a variable of its own, different from the variableemp
in yourmain()
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()
andmain()
. But if you assignnull
to the argument variable of the methodchangedetails()
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.