Wednesday, 2 November 2016

Class Loader in Java

Java ClassLoaders are used to load classes at runtime.
Java ClassLoaders  work on 3 main principles.

1.Delegation
2.Visibility
3.Uniqueness

Delegation forwards the request of  class loading to Parent Class Loader and only loads the class when Parent ClassLoader is not able to find or load the Class.

Visibility helps child loader to see the classes loaded by Parent Class.

Uniqueness allows any class to load exactly once.
This is achieved by the help of Delegation.
It ensures that child ClassLoader does not load the class which have been already loaded by Parent ClassLoader.

What is Class Loader in Java?
ClassLoader is a class which is used to load class files. Java compiler javac compiles java files into class files. Then JVM executes byte code in class files. ClassLoaders are responsible for loading classes from System or Network.

Default Functioning of ClassLoader -
A ClassLoader's basic purpose is to service a request for a class. The JVM needs a class, so it asks the ClassLoader, by name, for this class, and the ClassLoader attempts to return a Class object that represents the class.

 There are 3 default ClassLoaders in Java

1.BootStrap  ClassLoader
2.Extension  ClassLoader
3.System ClassLoader

Every ClassLoader has predefined function from where they loads the classes.
Bootstrap ClassLoader is responsible for loading standard JDK class files from rt.jar
It is the Parent of all ClassLoaders.
(*note - rt.jar file is present in jre/bin)

Extension ClassLoader delegates loading request to Bootstrap, if unsuccessful loads classes from
dir jre/bin/ext .

System ClassLoader is responsible to load classes from classpath or -cp command line option or
Class-Path attribute inside JAR
  
Except Bootstrap all class Loaders are implemented by java.lang.classLoader.
Bootstrap is implemented in C.






No comments:

Post a Comment