File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff b/w new and class.forName

 
Abhijith Prabhakar
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me what is the difference of creating an object using new operator and Class.forName(className).newInstance()?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 8791
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    Joanne Neal
    Rancher
    Pie
    Posts: 3742
    16
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic