Java ClassLoader
The Java Classloader is a part of the JRE (Java Runtime Environment) that dynamically loads Java classes into the JVM (Java Virtual Machine). In particular, a Java program, unlike one written in C or C++, isn't a single executable file, but instead is composed of many individual class files, each of which corresponds to a single Java class. Normally classes are only loaded on demand. This means, these Java class files are not loaded into memory all at once, but rather are loaded on demand, as needed by the program (Class Loader). Class Loader is a component with the Java Execution Engine which loads the Binary data from the .class files available in the classpath into the Method Area . Loading of a class into the method area occurs only the first time when the class is referenced with in the running Java application. For all other references the data is reused from the method area, unless the class has been UNLOADED .
ClassLoader in Java works on three principle:
- Delegation
- Visibility
- Uniqueness
All JVM (Java virtual machines) include one class loader that is embedded in the virtual machine. This embedded loader is called the primordial class loader . It is somewhat special because the VM (virtual machine) assumes that it has access to a repository of trusted classes which can be run by the virtual machine without verification. When the Java Virtual Machine is started, three class loaders are used:
- Bootstrap class loader
- Extensions class loader
- System class loader
Building a SimpleClassLoader
A class loader starts by being a subclass of java.lang.ClassLoader . The only abstract method that must be implemented is loadClass(). The flow of loadClass() is as follows:
- Verify class name.
- Check to see if the class requested has already been loaded.
- Check to see if the class is a "system" class.
- Attempt to fetch the class from this class loader's repository.
- Define the class for the Virtual Machine.
- Resolve the class.
- Return the class to the caller.
No comments:
Post a Comment