This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Assigning Object Ref Variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning Object Ref Variables." Watch "Assigning Object Ref Variables." New topic
Author

Assigning Object Ref Variables.

Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
Hi All,
Please explain me more about this. I am confused with this. Assuming that I have created a class Box.
Box b1 = new Box(); // My Understanding is We are creating a object b1 of type Box. - Is this Correct?
Box b2 = b1; // Here we are NOT Creating Object b2 but we are refering to Object b1
As per my understanding both b1 and b2 will refer to same memory space.(Object).
Now I am confused this line of code.
b1= null;
When we set b1 to null what happens to b2.As per the book i am reading, it still points to the original object.I was expecting b2 will also be null.
Please explain me more about this.
Thanks in advance,
Mubeen Shaik.


Sun Certified Java Programmer
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
b1 and b2 are independent variables that can either be set to refer to an object of type Box, or can be set to null (in which case they don't refer to any object).
When you set b1 = null, all you are doing is saying "b1 no longer refers to that Box object". It doesn't stop b2 referring to the object. You'd have to set b2 = null too, if you want to remove all references to the object.
Basically the object is separate from its references. You can have multiple references to the same object. In your case, both b1 and b2 refer to the same object. You could change one of those variables to point to a different object, or set it to null, and it won't have any effect on the original object or any of its other references.
Hope this hasn't confused you even more! It's quite difficult to explain, because once you understand it, it becomes second-nature.
David Peterson
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Mubeen
Your terminology is a little off...
Box b1 = new Box(); You are creating a new object of type Box and the variable b1 holds a reference to it (points to it).
Box b2 = b1; the variable b2 now refers to the same Box object as b1
b1= null; All this is doing is setting the variable b1 to null, it has no effect on the object b1 refered to. Think of it this way, when you set a variable equal (=) to an object like you did in the first line the variable gets a reference when you set it to null you are just replacing the reference with null, so b1 now references 'null' or nothing. The original object still exists and is still referenced by the value held in b2.
Even if there was no b2, setting b1 to ull would not effect the object it references except to make it eligible for garbage collection, the object would still exist but you'd have no way of getting a reference to it.
hope that helped out


Dave
Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
Hi All,
Thanks for the detailed explanation. Dave, Thanks for correcting my terminology. I think i am clear with that concept now. Once again i will read that concept.
Thanks,
Mubeen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning Object Ref Variables.
 
Similar Threads
Boolean Operator
Garbage Collection problem-2
Mock Question HFEJB page 426. Q4.
How many eligible for gc?
K&B SCJP6 Chapter 3- Q11 a GC question