Monday, December 1, 2008

Class Loader of Java


In Java class loaders are used to load a class from the local disk when JVM needs it.

The class loader obtains the information related to the class from the .class file which is formed once we compile the code from the local machine.

We can provide our own implementation of the class loader which will load a class from the remote machine but still the class file is first copied to the local disk and then the data is loaded.

Every class’ objects contain a reference to the class loader.

Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader () is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.

Class loaders are used by security managers to indicate the security domain.

Class loader class uses delegation model to search information about the classes.

Each instance of a class has a parent which calls the class loader to load it and becomes the parent of the class.

There is an exception to this rule

The JVM’s built in class loader “bootstrap class loader” does not have a parent but has classes to which it acts as a parent. (This is usually loaded from the file rt.jar)

Apart from this the other class loaders of the JVM are the “Extension class loader” (found usually from the jre/lib/ext directory) and the “System/Application class loader” (normally found in the CLASSPATH)

Additionally this class files are not loaded into memory all at once, but instead are loaded on demand, as and when required. Here comes Class Loader into the picture, it loads classes into memory.

Class Loader is simply a class written in JAVA language only. So in this way java gives you one more chance to create your own class loader rather than using the one that is already provided. 

No comments: