Showing posts with label classloader. Show all posts
Showing posts with label classloader. Show all posts

Wednesday, July 21, 2010

Class loading -static vs dynamic

Static Class Loading

  • Classes are statically loaded with Java's

"new" operator.

class MyClass {

public static void main(String args[]) {

Car c = new Car();

}

}

  • A NoClassDefFoundException is

thrown if a class is referenced with

Java's "new" operator (i.e. static loading)

but the runtime system cannot find the

referenced class.

Dynamic loading


  • Dynamic loading is a technique for programmatically invoking the functions of a

class loader at run time. Let us look at how to load classes dynamically.

Class.forName (String className); //static method which returns a Class

The above static method returns the class object associated with the class

name. The string className can be supplied dynamically at run time. Unlike the

static loading, the dynamic loading will decide whether to load the class Car or

the class Jeep at runtime based on a properties file and/or other runtime

conditions. Once the class is dynamically loaded the following method returns an

instance of the loaded class. It's just like creating a class object with no

arguments.

class.newInstance (); //A non-static method, which creates an instance of a

//class (i.e. creates an object).

Jeep myJeep = null ;

//myClassName should be read from a .properties file or a Constants class.

// stay away from hard coding values in your program

String myClassName = "au.com.Jeep" ;

Class vehicleClass = Class.forName(myClassName) ;

myJeep = (Jeep) vehicleClass.newInstance();

myJeep.setFuelCapacity(5);


  • A ClassNotFoundException is thrown when an application tries to load in a

class through its string name using the following methods but no definition for the

class with the specified name could be found:

The forName(..) method in class - Class.

The findSystemClass(..) method in class - ClassLoader.

The loadClass(..) method in class - ClassLoader.

Java class loaders

Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So, how is the very first class loaded? The very first class is especially loaded with the help of static main( ) method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now let's look at non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. Let us look at the class loaders created by the JVM.




class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true