Tuesday, 18 March 2014

Java Class Loading Concept

Java ClassLoaders

What is a class Loader?

In java ,Class Loader is one who is responsible for loading the corresponding class files. JVM itself not load the classes, rather when ever it find a dependent class, it goes to classLoader as ask him to load the corresponding class file.

Types of class Loader

In java Class Loader functionality differ for both core and J2E

There are 3 types of basic core Class Loaders-

  • Bootstrap ClassLoader
  • Extension ClassLoader
  • System ClassLoader

Bootstrap ClassLoader-

  • This is parent of all class loaders.
  • In java its also called as native class loader

Functions:-

Bootstrap ClassLoader load the classes that are present in  {$JAVA_HOME}/jre/lib/rt.jar , i.e
it load the default java classes (forex- java.lang.String etc.) , that are required during any other java classes..
Bootstrap ClassLoader is specific for each platform.
It load platform dependent classes.

Extension ClassLoader-

  • This is parent of System ClassLoader and child of Bootstrap ClassLoader .

Functions:-

Extension ClassLoader load the classes that are present in  {$JAVA_HOME}/jre/lib/ext folder
Any class that is found in ext folder is loaded by Extension loader.

System ClassLoader-

  • This is child of Extension ClassLoader and Bootstrap ClassLoader .

Functions:-

System ClassLoader load the classes that are present in  Classpath and -cp  and from classpath attribute
present in manifest file.

Principle of Class Loading

In java class loading mechanism executed on the basis of 3 principle's mentioned below

  • By principle of Delegation
  • By principle of Visibility
  • By principle of Uniqueness

Principle of Delegation

In java when ever JVM want a class to load, it always ask System class loader to find and load the class, but instead of looking for and loading the class, system class loader first delegate it to its parent class loader, i.e extension class loader. Similarly extension class loader delegate the request to its parent i.e bootstrap loader. As bootstrap is the supreme parent it look for the class in rt.jar and if not found throw an exception, then request comes to extension class loader again, now it will check class in ext folder, and in system property, if not found throw an exception, then request again comes to system class loader, now it will check the class file in classpath and -cp. if it finds the class it will load it else throws an exception. Thus this complete cycle obey principle of Delegation.

Principle of Visibility

As all class loader are arranged in parent and child hierarchy, no parent class loader can see the classes loaded by child class loader, but vice versa is possible.

Principle of Uniqueness

It states that no two class loader can load same class.

 Is principle of Uniqueness always follow??

Is really Principle of Uniqueness always follow, or there are some ways we can violate it!!!!!!!!!!!!!!!!!!!!!!!!!.

Consider the below Scenario...

 Class TestLoader
 {
   public static void main(String[] args) throws Exception 
   {
   // we can create object by two ways-
   Hello h=new Hello();
   Hello h1=new TestLoader().getLoaderClass("Hello");  --line 4
   }
   
   // method
    public Object getLoaderClass(String clsname) throws Exception
     {
         Object obj=Class.forName(clsname,true,this.getClass().getClassLoader());
         return obj;
     }
 }
  

Suppose we have class name Hello.java, that is present in both classpath and ext folder.
Now when main executed JVM find hello class, so he went to system class loader and ask it to load Hello file. System loader follow delegation principle and delegate the request, this process goes upto to supreme parent. As bootstrap loader will not found the classes, request again comes to extension loader. As class is present in ext folder, so its load the Hello class. But again in (line number 4), we are again creating class object, but this time we are explicitly telling to JVM to load my classes through current class loader. So this time the class is directly loaded by System Class Loader, and so ,delegation principle is ignored, and now single class is loaded by two class loader.

But behind the screen this is not happen and principle of Uniqueness endured because of NameSpaces

Let see what is NameSpace and what is does??

Whenever a classLoader load a class, it will place that class in NameSpace by prefixing that class with its name.(for ex- ExtensionClassLoader.Hello). So even in above case Hello class is loaded by Extension as well as System classLoader, they both will store as-

  • ExtensionClassLoader.Hello
  • SystemClassLoader.Hello
So both of them treated as a different class, and thus Principle of Uniqueness Obeyed.

Hierarchy of classLoaders

Hierarchy of classLoaders Boot ClassLoader Extension ClassLoader System ClassLoader

Phases of Class Loading

Phases of Class Loading

Apart from all the basic core class loader, also there are some other class loader present.

But point to remember here is functionality of Core class Loader is different from J2E class Loader.

In J2E there is dedicated class loader to load the classes. The implementation of these dedicated loader depend upon the server vendor.

In a J2E environment we can create three types of appplication.ie...
  • jar(Collection of classes)
  • war (Collection of classes and web resource)
  • ear(war+jar)
jar
|-Classes
war
|-web resources
|-jar
ear |-war
|-jar
So in J2E Environment we can say that their are basically 3 dedicated loader.
  • jar loader
  • war loader
  • ear loader

These loader are internally using each other.
Lets take a Scenario..................
In an enterprise application their is --
'n' number of war and
'n' number of jar
Suppose we have 2 war and 1 jar in an enterprise application. So for that their is 2 separate war class loader and 1 separate jar loader. which implies that all war related class are loaded by separate war loader, and all jar related classes are loaded by jar loader in this enterprise application.



Just Create two servlet in two different web application and use the
common singleton class in both servlet, and then print the hashcode for Singleton class,
u will find two different hashcode of a singleton class.

Some Facts related to class loading, and their impact

Let discuss Singleton class, and how class loading impact it..

What actually a Singleton class is?

Basic Definition (Applicable to core level):-
A class is called as Singleton only when,for that class their must be only 1 object per JVM exist.

  
 public class Singleton implements Cloneable {
 private static Singleton instance;

 private Singleton() {
  // no-op
 }

 public static synchronized Singleton getInstance() {
  if (instance == null) {
   System.out.println("first time object creation");
   instance = new Singleton();
  }
  return instance;
 }

 /*
  * (non-Javadoc)
  * @see java.lang.Object#clone()
  * 
  * overriding clone() method and throwing CloneNotSupportedException
  */
  public Object clone() throws CloneNotSupportedException
  {
  // return super.clone();
  throw new CloneNotSupportedException("This class prohibited Cloning");
 
  }
}

  

The above class will generate a single object per JVM at core level. But if we use the same class at web application level i.e if we use it for two separate web application , then you will find that their will be more than 1 object for that class exist. And the Reason behind that is the class loading mechanism of dedicated class loader in J2E.

So the more effective way to Define of Singleton Class is.............

A Singleton Class is a class that have 1 object per scope of a dedicated class loader or application Level.

No comments:

Post a Comment