There are three things that need to happen before you can "use" an instance of a class, each one of which entails allocation of heap memory:
The classes bytecodes need to be loaded and linked to resolve any static dependencies on other classes.
The class needs to be initialized.
An instance of the class needs to be created.
The loading and linking of classes typically happens when you start the JVM, though it can be done "lazily" by the JVM, and it can be done dynamically; e.g. using Class.forName(...)
. This is when memory for the classes "code" is allocated.
Class initialization is normally done immediately before the first time that the class is actually used. (The precise details are set out in the JLS). This is when memory for a classes statics will be allocated.
Class creation happens when the new
construct is used. This will also trigger class initialization for a class that has not yet been initialised. This is when memory for an instance is allocated.
In addition to the above, at some point the JVM may run the JIT compiler to turn the bytecodes for a class into native code. When (and indeed if) this happens depends on the JVM version and the JVM launch options. JIT compilation will of course allocate memory to hold the classes compiled native code.