• Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens to these objects?  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that something like String s = "JavaRanch"; has a string s that points to a memory location where the object "JavaRanch" is stored in the String pool (I think this is right).

However, when I call something like this:



What exactly maintains a reference to the Object in changeIt(str)? Is it s? Is it immediately collected by the garbage collector since there is no reference type/variable in main that references it? When I see things like this, and other objects like:



I get confused. Are these new objects just printed once, then destroyed in garbage collection since there is no possible way to reference them? Perhaps Strings aren't the best example since they are kept in the String pool, but what about the FileReader object? As I never assigned it a variable name, I don't see how I can possibly reference it again. What happens to this?

Thanks!
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the string pool complicates things so we'll ignore it for the sake of discussion.

Any object reference in a static scope will not be collected as the reference is in-scope as long as the class is loaded.

Similarly, any object referenced from main() will not be collected as main is in-scope for the entire program. Calling a method from another method does not cause the calling method to go out of scope. If it did, any variables set before the call could not be referenced afterwards.
 
Robert Lippens
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yeah, so it doesn't get collected until the actual class is no longer loaded. However, I still will never be able to access it again, correct? If there is no active reference to it, I have no way of getting back to it, even though it won't be deleted? It just sits there, floating in the endless expanse of 1's and 0's, forever lost?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Lippens wrote:Ah yeah, so it doesn't get collected until the actual class is no longer loaded. However, I still will never be able to access it again, correct?

It's a static reference. It never goes away. Why would you be unable to access it?

If there is no active reference to it, I have no way of getting back to it, even though it won't be deleted?

There is an active reference.What makes you think that the static variable will somehow disappear?

It just sits there, floating in the endless expanse of 1's and 0's, forever lost?

No. See above.
 
Robert Lippens
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah oops, you answered it for me - for some reason I was wondering how to access changeIt(str) but it doesn't return anything so there is nothing to access again later in the code (I could call the method again over and over though)! If changeIt(str) had had a return type of String then I would have to make a call in main like

String changed = changeIt(str);

so if I had ever wanted to refer to that new String object I made in changeIt, I could just use the variable changed, which points to the String object. I thought I had some object made with no reference to it, but all I did was print something to the screen. Too many hours of studying polymorphisms earlier, I guess...

I guess I just needed to sleep on it and let my brain process things. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!