Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference b/w class.forName

 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the Difference between
class.forName("MyClass.class") and
MyClass myclass=new MyClass();
 
Java Brewer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic