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 !
The issue is your constructor is not public
so you will need to use getDeclaredConstructor().newInstance();
or make the constructor public
.
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.