File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Class.forName vs new Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class.forName vs new " Watch "Class.forName vs new " New topic

Class.forName vs new

Sonalee Mohapatra

Joined: Sep 15, 2003
Posts: 10
what is the difference between Class.forName and new keyword while instantiating a class ?
Lakshmi Dasari
Ranch Hand

Joined: Mar 03, 2003
Posts: 35
Class.forName is used to dynamically "load" a class. Its predominently used to load jdbc drivers. In this way loading and creating an instance of a class can be deferred to runtime. Whereas creating an instance using "new" operator requires that the class name be known at compile time itself.
[ September 15, 2003: Message edited by: Lakshmi Dasari ]

Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Be aware that there is a clear distinction between the loading of a class, which may be triggered when one of its static methods is invoked, and the instantiation of a class with the new keyword. It is quite possible, and in some cases highly desirable, to have a class loaded but never instantiated.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
norman richards
Ranch Hand

Joined: Jul 21, 2003
Posts: 367
To be a bit clearer, Class.forName("java.lang.String") is the exactly same is writing: java.lang.String.class .
So, if you understand the difference between "String.class" and "new String()" then you should be able to answer the question.
Gary Mann
Ranch Hand

Joined: Jun 05, 2003
Posts: 37
As described above, the "new" method requires that the class name be known at compile time and creates an instance, whereas forName is used at runtime and only loads a class. This is not just useful for jdbc drivers, it is useful for whenever you want to specify a class name dynamically. e.g. on initializing a web app, you could read the names of classes as parameters and load them via forName. This enables different implementations to be used without code changes.

forName also allows specification of the class loader and whether the class is initialized.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
forName requires a no-parameter constructor. If you want to dynamically create a class with a more complex constructor, you need to dive deeper into reflection. Here's a little Interpreter project that goes all the way.
For those of us who think typed langauges are a good thing because they let the compiler catch more errors, dynamic creation is not something we do lightly. Errors like incorrect classnames might be introduced in the field by configuration typos. But it can be way cool. I'm writing a web server now with plug-in macro processors - just give the classname in the configuration and I create a singleton at startup.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sonalee Mohapatra

Joined: Sep 15, 2003
Posts: 10
thanks all for the replies !!
regards, sonalee
I agree. Here's the link:
subject: Class.forName vs new