1

I have object e of class EMP. I add it to array and make the reference of e as null.

Emp e = new Emp();
Emp[] lst = new Emp[10];
lst[0] = e;
e = null; 
System.out.println("Emp object = "+l[0]);

What I know is array lst doesn't contain the actual empolyee object but reference to employee object. So If I make the reference e as null then how are we still able to access the object using lst[0].

3 Answers 3

4

e is a reference to an object as well, or to be precise: a variable holding a reference.

So you have two references pointing to the same object. You "null-ify" only the first one. The real object still exists, and as it is still referenced through that array, it will also not be collected by the garbage collector.

That is all there is to this.

Sign up to request clarification or add additional context in comments.

2 Comments

How do we have two reference pointing to same object ? Does lst[0]=e; means we are creating a copy of reference e and storing it at the array location lst[0] ?
You could say so. Imagine that a reference simply points to a memory location. You can have as many such "pointers" as you want.
3
Emp e = new Emp(); // e points to some object
ListEmp lst = new ListEmp[10];
lst[0] = e; // lst[0] points to some object which is the same object pointed to by e
e = null; // here you are making e to point to null but lst[0] is still pointing to the object that was previously pointed to by e 

Neither e nor lst[0] are the actual objects. They are just references to the same object. So making a reference null doesn't make the object itself null. So if there are any existing references to the object, it'll still stay in memory.

Comments

2

Reference is simply a number which JVM uses to identify and locate objects. When you do

Emp e = new Emp();

new Emp(); creates instance of Emp class and returns reference to that instance - lets say it is 123. Then via Emp e = that identifier is assigned to reference variable e, so it holds that 123.

Later at

lst[0] = e;

123 (reference) will be copied to from e to lst[0].

BUT e and lst[0] are separate variables even if they are holding same values. Changing value at one of them (to hold null reference), doesn't mean automatically changing value of another.

In other words, when you do lst[0] = e; it doesn't mean that lst[0] is told to always use value held by e, but that it should take a look at current value held by e and copy that value (once). So when you later change value held by e (even to null) it will not change what lst[0] holds.

2 Comments

How do we know that reference is copied when lst[0] = e is executed ? I know because if this was wasn't the case the behaviour would be different. Is there some reference/article where I can get to know what exactly is happening at this step.
@ashish2199 I don't remember where I read about it. Sorry. But probably Java Language Specification also holds the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.