• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Assigning Object Ref Variables.

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic