Java Class Loading Basics

Classes are generated by Java compiler and JAR is a container for classes. A Java class loader loads the java classes into JVM (Java Virtual Machine). If the class loader is asked to load a class - it will first check whether the class is already loaded or not, if class is not loaded then it asks the parent to load the class. If parent cannot load, it will attempt to load the class by itself. When a class is loaded, all classes it refers are also loaded. Unreferenced classes will not be loaded until they are referenced.

A lot happens when a java class file is loaded
a. Decoding the binary format and verifying it.
b. Checking compatibility
c. Instantiating java.lang.Class to represent the loaded class.

When ever a class load request occurs, the Class loader looks for a .class file with the name of the class passed. Every class loader has a parent, except "Bootstrap Class Loader" - JVM's built in class loader. Normally class files are loaded from local file system in a platform dependent manner, but they can also be loaded from network.

When JVM is started, the bootstrap class loader will load bootstrap classes like
a. runtime classes in rt.jar
b. internationalization classes in i18n.jar etc.

Extension class loader loads classes from lib/ext directory. System class path class loader loads classes from the CLASSPATH. class path will only be searched if class hasn't been found among classes in rt.jar or extensions.

Loading a class dynamically is easy, for this you need to obtain a ClassLoader and call its loadClass() method.