Thursday, June 13, 2013

Eager and Lazy Loading


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 thejava.lang.Class.forName() method in the Java™ API, or through the creation of a user class loader.


No comments: