No wonder.
When you define an enum type (in your case Enum1 ) and declare values ββfor it, their reference type will have a specific type ( Enum1 ). When you define methods in your enum declarations, you have just created anonymous subclasses .
public enum Enum1 { OVERWHELMING(16) { public String getName() { return "OVERWHELMING"; } };
And you cannot just properly invoke methods of an anonymous class outside the definition itself. Similarly, someOtherMethod() also not be accessible externally:
ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { someOtherMethod(); } public void someOtherMethod() { System.out.println("Some other method!"); } };
Such methods, however, are available from the declaration itself, for example as indicated in this answer .
Note: As Joachim Sauer in his comment , this may not be the best design. You must, for example, port your methods to type Enum1 .
There is another way. Access to the method is possible through reflection:
Class<?> clazz = Enum1.HUGE.getClass(); Method method = clazz.getMethod("getCountry"); System.out.println(method.invoke(Enum1.HUGE));
source share