This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Now, once I allocated obj2 to obj1, there is no use of obj2 anymore in memory. Then don't you think it should get garbage collected ? If my fundamental is not clear, please correct me.
jazy smith
Ranch Hand
Joined: Nov 18, 2009
Posts: 101
posted
0
Hi all,
The above code, after gc(), still prints the value of obj2. Now, once I allocated obj2 to obj1, there is no use of obj2 anymore in memory. Then don't you think it should get garbage collected ? If my fundamental is not clear, please correct me.
There are two references to obj2 now. One named obj1 and one named obj2.
The original obj1 is garbage collected.
When you do things right, people won't be sure you've done anything at all.
jazy smith
Ranch Hand
Joined: Nov 18, 2009
Posts: 101
posted
0
once class gets loaded into memory, it will return the starting address of the memory where it resides. this starting address is what an object of the class holds. am I correct ?
jazy smith wrote:once class gets loaded into memory, it will return the starting address of the memory where it resides. this starting address is what an object of the class holds. am I correct ?
No, loading a class into memory returns a Class object, if you do it with a ClassLoader's loadClass() method. However in regular programming you won't ever do that, so loading a class into memory just results in a class being in memory.
And no, an object of the class doesn't hold the address of that class. Internally it probably holds a reference to the class, but you won't ever see that in ordinary programming. What it really holds, from the programmer's point of view, is all static members of the class.
You seem to be focusing on all of the wrong things and thereby making up a completely inaccurate mental model of how things work. In particular "addresses of memory" is a useless concept for understanding Java. It's true that internally things know the addresses of other things, but for Java programming we don't care about that. Instead objects can contain references to other objects. Those may look like memory addresses but they might be more complicated than that; but again, we don't care about that.
jazy smith
Ranch Hand
Joined: Nov 18, 2009
Posts: 101
posted
0
Hi Paul,
you are absolutely correct. I think I am unnecessarily going into deep. I really appreciate you gave pretty perfect explaination. thanks a ton