Vikky
Vikky

Reputation: 1223

Class Loading in JVM

When does the class get loaded in the JVM? Do they get loaded on the server start up or when there is a reference for the class? My assumption is that all the class gets loaded as the server like jboss starts up but then there is something called lazyloading.

Also what is actually meant by loading? Does it mean there is this .class in the JVM memory along with all the methods, variables including instance, static variables methods and are available for execution. I Know that ClassLoader locates the bytecodes for a Java class that needs to be loaded, reads the bytecodes, checks the refrencces of other class used in the particualr class and loads them as well by creating an instance of the java.lang.Class class. This makes the class available to the JVM for execution

Are methods also loaded in the JVM along with the class? My assumption is that methods are only in the stack memory of threads. then What is method memory? Is it a part of heap or stack?

Do only static methods get loaded along with class loading and not the instance method? Iknow that static bock gets executed when the class get laoded and also all the static variables get initialzed.

Thanks in advance if these doubts get cleared.

Upvotes: 2

Views: 2728

Answers (1)

Ion Ionascu
Ion Ionascu

Reputation: 552

These are pretty much basic questions about JVM and Google could surely help you with answers.

For some of your questions (especially for the questions about the actual loading process), you could look here, for example: http://www.securingjava.com/chapter-two/chapter-two-7.html

On short, at the beginning, just the basic (and trusted) classes are loaded by the JVM. Next, other classloaders (for example the bootstrap classloader) are created as required and they will load some more classes. Before a class can be successfully loaded, all the classes it depends on must be loaded.

A loaded class is stored in memory in various forms (this is JVM specific), but a Class object is always exposed. Everything inside the class (methods, variables etc.) gets loaded. This doesn't mean that the class is also compiled (compilation happens later, when a method needs to be executed).

Allocation of method variables happens either on stack (for primitives) or on heap.

Initialization of static variables and execution of static blocks happens right after the class is loaded, before any instances of it are created.

Upvotes: 2

Related Questions