• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC behavior towards static objects

 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm kind of curious to know about GC behavior towards static objects? Does GC collects static objects too and those objects that are initialized in static initializer or static methods?
For e.g.,
class A {
static { Object o = new Object();}
static void show() {Object p = new Object();}
}
In the following class, when will these two objects be available for GC? Does the normal rules apply here also.
Thanks,
Deep
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object cannot be static, but a reference to it can. An object is created on the heap and is eligible for garbage collection as soon as there is no more reference to it in the program.
In your code, the object referenced by o is eligible for GC at the end of the static block and the one referenced by p at the end of the static method show().
[ January 07, 2003: Message edited by: Valentin Crettaz ]
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one suggest a good material on GC.Am finding it hard to follow.I guess M.khalid is not enough.
Thank you.
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the detailed responses.
deep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic