Say you have
static class Foo
{
static Bar bar = new Bar();
static int func(){ ... }
}
obviously, a Foo
object will not be created for calling func()
.
However, the class Foo
needs to be loaded in memory; and to the application, there is an object corresponding to the class, which can be referred to as Foo.class
, or Class.forName("Foo")
.
A loaded class is not initialized yet. When you call a static method for the 1st time, the class is initialized; some "space" is allocated for static variables, and static initializer code (like new Bar()
) is executed.
This "space" is not visible to application as an object; but it's an in memory data structure too that concerns garbage collections (and other objects it refers to, like bar
)
The class, and the "space", are only eligible for GC when the classloader that loaded the class is eligible for GC. For usual command line applications, that never happens. But for a lot of other applications, class GC is important, and class loading needs to be carefully done.