Get Constructor
You can access the constructors of a class like this:-
Using Java Reflection you can inspect the constructors of classes and instantiate objects at runtime.
This is done via the Java class java.lang.reflect.Constructor.
This text will get into more detail about the Java Construcor object.
The Constructor[] array 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 do so rather than obtain the array all constructors.
This example returns the public constructor of the given class which takes a String as parameter:-
If no constructor matches the given constructor arguments, in this case String.class, aNoSuchMethodException is thrown.
The
In this case it was a constructor taking a String, so one String must be supplied.
Constructor[] constructors = aClass.getConstructors();
Using Java Reflection you can inspect the constructors of classes and instantiate objects at runtime.
This is done via the Java class java.lang.reflect.Constructor.
This text will get into more detail about the Java Construcor object.
-
Here is a list of the topics covered:-
- Obtaining Constructor Objects.
- Constructor Parameters.
- Instantiating Objects using Constructor Object.
1)Obtaining Constructor:
Objects The Constructor class is obtained from the Class object.Example:-
Class aClass = ...//obtain class object
Constructor[] constructors = aClass.getConstructors();
Constructor[] constructors = aClass.getConstructors();
The Constructor[] array 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 do so rather than obtain the array all constructors.
This example returns the public constructor of the given class which takes a String as parameter:-
Class aClass = ...//obtain class object
Constructor constructor = aClass.getConstructor(new Class[]{String.class});
Constructor constructor = aClass.getConstructor(new Class[]{String.class});
If no constructor matches the given constructor arguments, in this case String.class, aNoSuchMethodException is thrown.
2)Constructor Parameters
You can read what parameters a given constructor takes like this:- Constructor constructor = ... // obtain
constructor - see above
Class[] parameterTypes = constructor.getParameterTypes();
Class[] parameterTypes = constructor.getParameterTypes();
3)Instantiating Objects using Constructor
Object You can instantiate an object like this://get
constructor that takes a String as argument
Constructor constructor = MyObject.class.getConstructor(String.class);
MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");
Constructor constructor = MyObject.class.getConstructor(String.class);
MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");
The
Constructor.newInstance() method takes an optional amount of
parameters, but you must supply exactly one parameter per argument in
the constructor you are invoking. In this case it was a constructor taking a String, so one String must be supplied.


