1

Possible Duplicate:
Are static fields open for garbage collection?

If an object has a static reference in some class, will that object never be garbage collected? Also at what point will a static field be initialized?

Community
  • 1
  • 1
Rnet
  • 4,796
  • 9
  • 47
  • 83
  • 2
    Same as http://stackoverflow.com/questions/453023/are-static-fields-open-for-garbage-collection – Santosh Jan 04 '12 at 08:26

2 Answers2

1

A static field is initialised when the class is loaded. It is available to be GCed when its class loader is unloaded.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Well, this has been asked on SO multiple times. Since these are two questions, let's break it down:

Static variables are garbage collected when the ClassLoader through which they are loaded is electable for garbage collection. Check this post: Are static fields open for garbage collection?

As for initialization, they are initialized once the class holding them is first loaded. Check this post also: Why static fields are not initialized in time?

Community
  • 1
  • 1
Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72