Hello!
Hey, all! ^_^
I'm trying to pass an instance of Method into an enum constructor, but the compiler is telling me there will be a compile error because of an unreported exception.
Exception: "unreported exception java.lang.NoSuchMethodException; must be caught or declared to be thrown."
java version 6 update 5
public class Whatever{
public static enum PhoneType
{
MAIN_PHONE(Person.class.getDeclaredMethod("getPhone", new Class[0])),
OTHER_PHONE(Person.class.getDeclaredMethod("getOtherPhone", new Class[0])),
CELL_HONE(Person.class.getDeclaredMethod("getCellPhone", new Class[0]));
private Method method;
PhoneType(Method method)
{
this.method = method;
}
public Method getMethod()
{
return method;
}
public void setMethod(Method method)
{
this.method = method;
}
}// end enum PhoneType
}// end class whatever
I tried this:
MAIN_PHONE(try{Person.class.getDeclaredMethod("getPhone", new Class[0]);}catch(Exception e){}),
Yeah, that made compiler go nuts..
and this:
try{
MAIN_PHONE(Person.class.getDeclaredMethod("getPhone", new Class[0])),
OTHER_PHONE(Person.class.getDeclaredMethod("getOtherPhone", new Class[0])),
CELL_HONE(Person.class.getDeclaredMethod("getCellPhone", new Class[0]));
}
catch(Exception e){}
Even tried putting "throws Exception" on after the enum declaration and constructor. (/me == desperate ><) No workie.
Is there a way to solve this problem? If so, how?
Thanks!