Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static and dynamic class loading?

Tags:

java

oop

Why I need to load the class definition like :

Class.forName("ClassName");

What is the need and advantage of this .Typically which is used to load driver class in JDBC.

like image 221
JavaUser Avatar asked Jun 27 '10 03:06

JavaUser


People also ask

What is the difference between static and dynamic loading?

The simplest definition of a static load vs. a dynamic load is that static loads don't move and dynamic ones do. In the context of the supply chain, a static load refers to a loaded pallet on the floor, while a dynamic load is a loaded pallet being moved by a forklift, pallet jack, or other equipment.

What is dynamic class loading?

Dynamic Class Loading allows the loading of java code that is not known about before a program starts. Many classes rely on other classes and resources such as icons which make loading a single class unfeasible. For this reason the ClassLoader ( java. lang.

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

private, final and static members (methods and variables) use static binding while for virtual methods (In Java methods are virtual by default) binding is done during run time based upon the run time object. The static binding uses Type information for binding while Dynamic binding uses Objects to resolve to bind.


2 Answers

What is the need and advantage of this. Typically which is used to load driver class in JDBC.

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

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.

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(...).

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.

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.)

However, if you don't need to be able to do this kind of thing, static dependencies are simpler to implement.

FOLLOWUP

But here ,while compile itself the "ClassName" parameter is known .So the key external dependency is compiled into application source-code??

Nope. Obviously, that defeats the purpose. The application (or the framework) typically determines the names of the classes to be dynamically loaded from some configuration file.

like image 178
Stephen C Avatar answered Oct 21 '22 04:10

Stephen C


You don't, really. ClassName.class will work just as well. Anyway, getting the class definition in this manner is generally the way that SPI implementations are injected into APIs.

like image 36
Chris Dennett Avatar answered Oct 21 '22 04:10

Chris Dennett