Programming

(Java) java.lang.reflect.Constructor.newInstance : 자바 클래스 인스턴스를 동적으로 생성하기

steloflute 2013. 3. 4. 23:30

http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...)

자바 클래스 인스턴스를 동적으로 생성하기

 

newInstance

public T newInstance(Object... initargs)
              throws InstantiationException,
                     IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException
Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.

If the number of formal parameters required by the underlying constructor is 0, the supplied initargs array may be of length 0 or null.

If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.

If the required access and argument checks succeed and the instantiation will proceed, the constructor's declaring class is initialized if it has not already been initialized.

If the constructor completes normally, returns the newly created and initialized instance.

Parameters:
initargs - array of objects to be passed as arguments to the constructor call; values of primitive types are wrapped in a wrapper object of the appropriate type (e.g. a float in a Float)
Returns:
a new object created by calling the constructor this object represents
Throws:
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
IllegalArgumentException - if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum type.
InstantiationException - if the class that declares the underlying constructor represents an abstract class.
InvocationTargetException - if the underlying constructor throws an exception.
ExceptionInInitializerError - if the initialization provoked by this method fails.

 

'Programming' 카테고리의 다른 글

(Clojure) Creating an empty string array  (0) 2013.03.06
(Java) Lesson: Using Swing Components  (0) 2013.03.05
(Clojure) Calling clojure from java  (0) 2013.03.04
(Clojure) Invoking Clojure from Java  (0) 2013.03.03
The Implementation of Lua 5.0  (0) 2013.03.03