Call constructor

suggest change

Getting the Constructor Object

You can obtain Constructor class from the Class object like this:

Class myClass = ... // get a class object
Constructor[] constructors = myClass.getConstructors();

Where the constructors variable will have one Constructor instance for each public constructor declared in the class.

If you know the precise parameter types of the constructor you want to access, you can filter the specific constructor. The next example returns the public constructor of the given class which takes a Integer as parameter:

Class myClass = ... // get a class object
Constructor constructor = myClass.getConstructor(new Class[]{Integer.class});

If no constructor matches the given constructor arguments a NoSuchMethodException is thrown.

New Instance using Constructor Object

Class myClass = MyObj.class // get a class object
Constructor constructor = myClass.getConstructor(Integer.class);
MyObj myObj = (MyObj) constructor.newInstance(Integer.valueOf(123));

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Reflection API:
* Call constructor

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
44 Reflection API
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap