Reputation: 21865
Given class Award :
public class Award {
/*
*
*/
// fields of the class
Award()
{
// some initializations
}
I'm trying to invoke this constructor from Main :
try
{
Award myAward = Award.class.getConstructor().newInstance();
myAward.calculateAward();
}
catch (Exception e)
{
e.printStackTrace();
}
but it goes into the exception block and produces NoSuchMethodException exception .
What's wrong ?
Thanks !
Upvotes: 1
Views: 615
Reputation: 43852
According to the Javadoc:
The constructor to reflect is the public constructor of the class represented by this Class object whose formal parameter types match those specified by parameterTypes.
You constructor may need to be public. Add the public
keyword before your Award
constructor and try again.
Upvotes: 2
Reputation: 1036
The issue is your constructor is not public
so you will need to use getDeclaredConstructor().newInstance();
or make the constructor public
.
Upvotes: 9