Class Loader In Java
Class Loader
Java class loader is used to load a Java class files into Java virtual machine. It is a sub system of JVM.
Various types of Java Class Loaders in Java are,
- Bootstrap Class loader.
- Extensions Class loader.
- System Class loader.
Bootstrap Class loader
Bootstrap class loader is mainly used to load Java classes like java.lang, java.util etc. All the classes are the part of JRE (Java Runtime Environment). It is a native implementation in Java.
Extensions Class loader
Extensions class loader loads the classes from ext folder. When we use the system environment property java.ext.dirs, we can add these ‘ext’ folders and jar files to be loaded, using extensions class loader.
System Class loader
System classloader loads the classes, which are available in the Java classpath and these are loaded , using system class loader.
All class loaders have a hierarchical relationship between them. Class loader can load the classes from one level above its hierarchical.
First level is a bootstrap class loader.
Second level is extensions class loader.
Third level is a system class loader.
- How Java class loader works internally
When we give a class name, class loader first checks the location of the class, reads a class file of that name from the native file system. It is a subsystem of JVM. Thus, this loading process is platform dependent.
In Java, by default, java.lang.ClassLoader is registered as a classloader. This is sufficient to load the classes in parallel. The subclasses do not register i.e these classes need to register as parallel or not at the time of instantiation. In Java, we can also load the classes from network and constructed on runtime. Classloader has a method name defineClass(), which takes an input as a byte array and loads a class.
Rule of class loader
One class is loaded in JVM at single point of time.
Summary
Thus, we learnt, Class loader is used to load Java class files into Java virtual machine and also learnt their types in Java.