What Is Class Loader In Java, It's Functions And Importance??

Any kind of programming help in java will be provided here. 4 engineers help to engineers help is a place for all to solve any kind of java programming query.
By: 4 ENGINEERS HELP
 
 
Spread the Word
Listed Under

Tags:
Java
Java Programming Help
Java Programming
Java Engineers Help

Industry:
Engineering

Location:
Delhi - Delhi - India

March 30, 2009 - PRLog -- Class loaders offer a very powerful mechanism by which classes can be loaded at runtime. Its power lies in its extensibility, i.e. custom classes needed for the application can be loaded in addition to the basic classes loaded by bootstrap loader. But with such powerful tool comes some security problems as well. Its important for application developers to understand the functioning of classloaders to make their application efficient and and it will also make debugging easy.


One of the main features of java is "Write Once and Run Anywhere". The
Java Virtual Machine (JVM) is responsible for loading and executing the code. For this purpose, it uses the java class loader. The java class loaders are responsible for loading appropriate classes in the JVM at the runtime. In a JVM, each and every class is loaded by some instance of a
java.lang.ClassLoader. The main feature of the classloader is that JVM.
doesn’t need to have any knowledge about the classes that will be loaded at runtime. ClassLoaders support features like hot deployment and run time platform e extensibility. The next section explains how a class loader works.

Class Loader Functioning:



A class file is the smallest unit that gets loaded by the class loader. It contains binary representation of a java class
and contains bytecodes and links to other class files that will be used. Class loader reads this bytecode and creates the instance of
java.lang.Class. When the JVM starts, initially only the class file is loaded and other classes are loaded as and when required by the JVM. In this process, JVM is initially
unaware of what classes will be loaded later on. Lazy loading plays a key role in providing dynamic extensibility to the Java platform.
In lazy loading, dependents are only loaded as they are specifically
requested.

Different class loaders are responsible for loading different classes from different repositories.

First the " bootstrap class " loader loads the key classes
Next comes the " java extension class "loader. It loads the libraries, which are not part of the core java run time.
The ExtClassLoader is responsible for loading all .jar files kept in the java.ext.dirs path.
Finally, developer ,can add his own custom classes needed by the application .
Figure 1 below explains hierarchy of different class loaders.



Figure 1: Hierarchy of class loaders

The process of loading of a class by a class loader is not standalone process. Its a 3-step process.

Loading
Linking
Initialization
Loading is the process of locating the binary representation of a type and bringing it into the JVM. Linking is the process of taking the type and incorporating it into the runtime state of the JVM so that it can be executed. Initialization is the process of executing the initializers of a type (static initializers for classes; static field initializers for classes and interfaces). Once a class becomes unreachable it is available for garbage collection.

The next section deals with how the requested class is searched and loaded by a class loader

How Class Loader Works:

When a client requests to load a particular class, a check is performed by the class loader to see whether the requested class is already loaded. If the class is already loaded then loaded class is returned and
the request ceases. However if the class is not loaded, then the request is sent to the parent class loader to search for the requested class. This request for search can go up to
the level of bootstrap loader (highest in the hierarchy). If the parent is
successful in finding the class then that class is returned and the
request ceases, but if that does not work out, then the current class
loader has to find the requested class.

Each class loader has a specific location for searching the class. e.g
bootstrap loader searches for folders, zips and jars. The methods that are
invoked to search for and load the requested class are

protected Class findClass (String className) throws ClassNotFoundException
public class loadClass (String className) throws ClassNotFoundException.
FOR MORE YOU CAN VISIT: http://4-engineers-help.blogspot.com/

# # #

4 engineers help to engineers help , a problem solving place for all.
Any programming problem will be solved here. You can visit to me @ http://4-engineers-help.blogspot.com/
End
Email:***@gmail.com Email Verified
Phone:9818858836
Zip:201306
Tags:Java, Java Programming Help, Java Programming, Java Engineers Help
Industry:Engineering
Location:Delhi - Delhi - India
Account Email Address Verified     Disclaimer     Report Abuse



Like PRLog?
9K2K1K
Click to Share