--------
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
.