• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class instance life  RSS feed

 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks.

Let's assume I have two classes - A and B. In one of B Class static methods I'm creating an instance of A Class.


Now I'm invoking the createBInstance method from the main by B.createBInstance().

In my understanding the new A instance ('obj') is leaving until the createBInstance method is executed and when it does the obj is cleaned by garbage collector. Make sense?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'obj' the reference variable gets allocated in local stackframe of the method "createBInstance". Its life ends as the execution involving the method ends... But the instance of A lives in heap... It can live even when the execution of method ends if reference to that instance is still there...

But in this context,

Since there is no reference for that instance, it will be marked by Garbage Collector for removal...
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a tiny application like that it is most likely that it remains when the JVM exits, and is deleted from memory by the operating system when the RAM used is released.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!