This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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
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
Joined: Jan 26, 2004
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com