The Java Class Loader is a JVM subsystem responsible for loading class files into memory when they are required during application execution. Instead of loading all classes at startup, it loads them on demand, which improves startup time and memory efficiency.
Key Points: • Class loading follows three main phases: Loading, Linking, and Initialization. • Java uses a parent delegation model to avoid loading the same class multiple times and to enhance security. • The main class loaders are Bootstrap ClassLoader, Platform (Extension) ClassLoader, and Application (System) ClassLoader. • Custom ClassLoaders can be created to load classes from external sources such as databases, network locations, or encrypted files.
Example: When you create an object using `new Employee()`, the JVM first checks whether the Employee class is already loaded. If not, the Class Loader locates the class file, loads it into memory, verifies it, and initializes it before object creation.
Code Example:
public class ClassLoaderDemo {
public static void main(String[] args) {
ClassLoader loader =
ClassLoaderDemo.class.getClassLoader();
System.out.println(loader);
System.out.println(String.class.getClassLoader()); // null (Bootstrap ClassLoader)
}
}Interview Tip: A concise interview answer is: The Java Class Loader is a JVM component that dynamically loads classes into memory when they are needed. It follows the parent delegation model and uses Bootstrap, Platform, and Application ClassLoaders to efficiently and securely load classes while preventing duplicate loading.