What is Class Loading?
Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.
When Class is Loaded?
- Class loading done by Class Loaders may eagerly load a class or does lazy load
- Eager loading happens as soon as another class references it and lazy load happens when a need of class initialization occurs.
- If a Class is loaded before it’s actually being used, it can sit inside before being initialized which may vary from JVM to JVM.
- However, it’s guaranteed by JLS that a class will be loaded when there is a need of static initialization.
Whenever the JVM determines it needs a class (to use its static variables, to create a new object, to use its static methods etc) it will load the class and static initialization blocks will run, static variables are initialized etc. This is (at least under normal circumstances) done only once
SomeClass.someStaticMethod(); //SomeClass is loaded (if its not already) SomeClass.someStaticVariable; //SomeClass is loaded (if its not already) SomeClass var=new SomeClass(); //SomeClass is BOTH loaded (if its not already) AND instantiated
Class loader actually loads byte code into JVM , runs static initializers when you want to use static fields within class without creating an instance of it, class must be loaded by class loader first.Default classloader in java is java.lang.ClassLoader
Class Loading is mostly unpredictable
When Class is Initialized?
Class Initialization happens after class loading. A class is initialized in Java when
- An Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.
- an static method of Class is invoked.
- an static field of Class is assigned.
- an static field of class is used which is not a constant variable.
- if Class is a top level class and an assert statement lexically nested within class is
Which classes are Eagerly Loaded and Lazily Loaded
Eagerly Loaded Classes are those the JVM must be able to load JVM class files. The JVM class loader loads referenced JVM classes that have not already been linked to the runtime system. Classes are loaded implicitly because: The initial class file – the class file containing the public static void main(String args[]) method – must be loaded at startup. Depending on the class policy adopted by the JVM, classes referenced by this initial class can be loaded in either a lazy or eager manner.
An eager class loader loads all the classes comprising the application code at startup. Lazy class loaders wait until the first active use of a class before loading and linking its class file.
The first active use of a class occurs when one of the following occurs: • An instance of that class is created • An instance of one of its subclasses is initialized • One of its static fields is initialized. Certain classes, such as ClassNotFoundException, are loaded implicitly by the JVM to support execution. You may also load classes explicitly using the java.lang.Class.forName() method in the Java API, or through the creation of a user class loader.
Based on the above rules, lazy class loading can be forced on the system.
Use java -XX:+TraceClassLoading to trace the loading of classes. Use java -XX:+PrintCompilation to trace when methods are JITed.