0

Having:

Class<? extends MyInterface> clazz = ...;

should I (and why) write:

Constructor<? extends MyInterface> ctor = clazz.getConstructor();
MyInterface object = ctor.newInstance();

or:

MyInterface object = clazz.newInstance();

Does the choice depend on either the constructor I intend to use is empty or takes arguments?

Queequeg
  • 2,824
  • 8
  • 39
  • 66

5 Answers5

1

According to the Javadoc of newInstance():

The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

Appearantly, you can use newInstance() as a shortcut for

Constructor<? extends MyInterface> ctor = clazz.getConstructor();
MyInterface object = ctor.newInstance();

There's a subtle difference however: Class#newInstance() will wrap an Excption from the constructor inside a ExceptionInInitializerError, whereas the Constructor#newInstance() will wrap it inside an InvocationTargetException.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • so I guess, constructor usage is safer since it throws an `Exception` and not an `Error` .. – Queequeg Mar 27 '13 at 13:57
  • Exceptions indicate a problem a program _might want to catch_, while an Error indicates a problem that a program _should not try to catch_. Whether your application should catch an Exception in this dynamically invoked constructor depends on the situation... – mthmulders Mar 27 '13 at 14:08
1

Take a look at this similar question. This link also gives some more examples and a more detailed explanation.

To sum up the arguments (from the second link):

    Class.newInstance() can only invoke the zero-argument constructor, 
    while Constructor.newInstance() may invoke any constructor, regardless 
    of the number of parameters.

    Class.newInstance() throws any exception thrown by the constructor, 
    regardless of whether it is checked or unchecked. 

    Class.newInstance() requires that the constructor be visible;  
    Constructor.newInstance() may invoke private constructors under certain 
    circumstances.

Sorry for editing my post to add more quotes, but this explains it better than I could. The Javadoc for Class.newInstance() explains it like this:

Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

Note that this method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException.

Community
  • 1
  • 1
John Kane
  • 4,383
  • 1
  • 24
  • 42
0

According to the source code of java.lang.Class Class.newInstance() is the same as Constructor.newInstance()

In the implementation the default constructor (the one without any parameters) is invoked

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
0

clazz.newInstance() actually attempts to get hold of the Constructor object, so it ends up being the same.

The first syntax allows instantiating classes using constructors with args.

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
0

The answer is obvious from a thorough reading of the javadoc for java.lang.Class.

  • The clazz.newInstance() approach only works with a no-args constructor.

  • The clazz.getConstructor(), etc approach also works with constructors with arguments ... provided that you supply the types and then the values of the constructor arguments.

If your use-case only requires you to use the no-args constructor, then clazz.newInstance() is the simpler approach.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216