File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Difference b/w class.forName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference b/w class.forName" Watch "Difference b/w class.forName" New topic
Author

Difference b/w class.forName

Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
What is the Difference between
class.forName("MyClass.class") and
MyClass myclass=new MyClass();


Thanks,
Mathews
Java Brewer
Greenhorn

Joined: Oct 13, 2003
Posts: 5
hi Mathews,

It the case of

The class is loaded into the JVM but you haven't got the handle to the Object of the class as such. To get the Object for the class you need to do


This approach is useful when you donno which class to load on the fly. Usually used in app. server relatedt applications.
Whereas in

You have explictly created and class Object and allocated the memory.
Regards
GoodyGuy
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
As a small example, from a factory that loads the classname to be loaded from a properties file based on a key handed in by the calling class:

handlers is a Properties Object loaded from a ResourceBundle.
handlerName is a String serving as a key into the ResourceBundle.
AbstractHandler is an abstract baseclass to all Objects that I expect to be able to load here (as the factory after creation calls an initialisation method on the created Object before returning it).
I devised this system to reduce the humongous length of some of our code (servlet classes consisting of 3000+ lines of code in a single unit) and split it up in smaller functional chunks.
This (apart from some basic code to initialise the passed parameters and errorchecking) is now all that remains of some 1500 lines in one class:


42
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
Thanks a lot
 
 
subject: Difference b/w class.forName