wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes diff b/w new and class.forName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "diff b/w new and class.forName" Watch "diff b/w new and class.forName" New topic
Author

diff b/w new and class.forName

Abhijith Prabhakar
Ranch Hand

Joined: Dec 29, 2006
Posts: 56
Can anyone tell me what is the difference of creating an object using new operator and Class.forName(className).newInstance()?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
New is an operator. It creates an instance of the class (or interface or array) whose name follows. Details can be inserted, like this
  • new int[12];
  • new int[]{1, 2, 3};
  • new FooClass();
  • new FooClass(1, 2, 3);
  • new BahInterface(){public void bahMethod1(){ . . .}public void bahMethod2{ . . .} };
  • . . . or something like that.

    Class.forName() gets a Class object which represents the class in the name. You pass a String with the fully-qualified name of the class as its argument.
    I am not quite sure what one can do with this Class object, but one thing possible is to get an instance of that class, assuming it has an accessible no-argument constructor. One also has to cast it.

    Foo myFoo = (Foo)Class.forName("mypackage.Foo").getIntance();

    or something like that. It is useful in factory methods where one wants to create objects of related classes, probably all in the same inheritance hierarchy, but if one needs any fields set up, they must be set up explicitly.

    Does that help?
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    The main difference is in dependencies. If I say

    I have to have SomeImplementation in my classpath at compile time. Changing the implementation can break my code ... cause it to not compile or not run. And I'm hard coded to a particular implementation. It would be hard to swap in a different class that implements SomeInterface.

    If I say:

    I can get the name from configuration or an argument and create an instance of some class that I never heard of before. This is a common way to do plug ins.

    Did that answer seem to fit the question?


    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
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3432
        
      12
    Originally posted by Campbell Ritchie:
    I am not quite sure what one can do with this Class object, but one thing possible is to get an instance of that class, assuming it has an accessible no-argument constructor.
    .
    .
    .
    It is useful in factory methods where one wants to create objects of related classes, probably all in the same inheritance hierarchy, but if one needs any fields set up, they must be set up explicitly.


    You can also use it to create instances of a class that doesn't have a no-arg constructor using the Class.getConstructor and Constructor.newInstance methods.


    Joanne
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: diff b/w new and class.forName
     
    Similar Threads
    Dynamically loading a class
    Class.forName
    what this method do in JDBC?
    Class.forname
    Difference b/w class.forName