Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we use Static and Dynamic class Loading?

Tags:

java

class

I know the difference between Static class loading and Dynamic class loading. In general,we always use Static class loading only.Can anyone tell, under which situations we use Dynamic class loading??

like image 873
Madhu Sudhan Reddy Avatar asked Oct 22 '13 13:10

Madhu Sudhan Reddy


People also ask

What is static and dynamic class loading?

What is static and dynamic class loading in Java? Static Class Loading: Creating objects and instance using new keyword is known as static class loading. The retrieval of class definition and instantiation of the object is done at compile time. Dynamic Class Loading: Loading classes use Class. forName () method.

What is the difference between static and dynamic class in Java?

Static loading refers to loading the whole program into the main memory before executing the program. Dynamic loading refers to the process of loading a program into the main memory on demand. It is only performed in structured programming languages such as C. It happens in OOPs languages such as C++, Java, and others.

What is class dynamic loading?

Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Dynamic class loading is done when the name of the class is not known at compile time.

Why do we use class loader?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.


2 Answers

Dynamic Class Loading allows the loading of java code that is not known about before a program starts. The Java model loads classes as needed and need not know the name of all classes in a collection before any one of its classes can be loaded and run.

For example : Depending on user input you want to create only one object and there are hundreds of classes. Then you don't need load all classes. You can create object at run time by dynamic class loading.

Code:

try {
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(in);

    System.out.println("Enter Class Name: ");
    String whatClass = reader.readLine();

    Class exampleClass = Class.forName(whatClass);
    Object ob = exampleClass.newInstance();

} catch (ClassNotFoundException e) {
    e.printStackTrace();

} catch (Exception e) {
    e.printStackTrace();
}
like image 90
nilesh.b Avatar answered Oct 01 '22 01:10

nilesh.b


DYNAMIC CLASS LOADING

It allows you to build your applications so that key external dependencies are not compiled into the application source-code.

APPLICATIONS

JDBC

For example, in the JDBC case, it allows you to switch between different driver implementations, and (in theory) different database vendors without changing your source code.

PLUG-INS

Another use-case is when some supplier develops a generic form of an application with extension points that allow customers to "plug in" their own custom classes. The custom classes are typically loaded using Class.forName(...).

FRAMEWORKS AND CONTAINERS

A third use-case is application frameworks and containers which typically use Class.forName(...) under the hood to dynamically load the classes for application-specific beans, servlets, and so on.

OTHERS

A fourth use-case is where the application (or more likely an application library) has modules that are not used in a typical application run. By using Class.forName(...) internally, the application or library can avoid the CPU and memory overhead of loading and initializing large numbers of unwanted classes. (The Sun Swing libraries apparently do this to reduce application startup times, and I'm sure there are other examples.)

Refer Dynamic Class Loading

like image 37
Prateek Avatar answered Oct 01 '22 00:10

Prateek