Junior — Middle
How does the class loading mechanism in JVM work and what types of ClassLoader do you know?
sobes.tech AI
Answer from AI
In the JVM, class loading is performed using the ClassLoader mechanism, which is responsible for searching, loading, and linking classes at runtime. When the JVM encounters a reference to a class, the corresponding ClassLoader loads the class's bytecode from a file or another source.
Main types of ClassLoader:
- Bootstrap ClassLoader — loads core Java classes from rt.jar (e.g., java.lang.*).
- Extension ClassLoader — loads Java extensions from the jre/lib/ext folder.
- System (Application) ClassLoader — loads classes from the application's classpath.
Custom ClassLoaders can be created to load classes from non-standard sources, such as from a network or database. Class loading occurs hierarchically: the request is first passed to the parent ClassLoader, and if the class is not found, it is loaded by the current one.