From: http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html 
There are two reflective methods for creating instances of classes: 
java.lang.reflect.Constructor.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29)
 and Class.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29).
 The former is preferred and is thus used in these examples because:
Class.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29)
 can only invoke the zero-argument constructor, while Constructor.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29)
 may invoke any constructor, regardless of the number of parameters.
Class.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29)
 throws any exception thrown by the constructor, regardless of whether it is 
checked or unchecked. InvocationTargetException 
(http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/InvocationTargetException.html).
Class.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29)
 requires that the constructor be visible; Constructor.newInstance() 
(http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29)
 may invoke private constructors under certain circumstances.




Thanks,
Hari

-- 
Hari Shreedharan


On Wednesday, September 19, 2012 at 10:20 PM, David Capwell wrote:

> I was going over the flume 1.2.0 code and i was wondering why
> the ComponentConfigurationFactory.create class has the following:
> 
> confType = (Class<? extends ComponentConfiguration>) Class.forName(type);
> return confType.getConstructor(String.class).newInstance(type);
> 
> Since type is the class, then why does the the class need a constructor
> that puts in the class name?
> 
> thanks for your time reading this email. 

Reply via email to