Those methods are methods of each individual enum instance, similar to a method declared within an anonymous class, and just like an anonymous class, the methods are not visible to variables of the parent type. Since ONE and TWO are instances of the MyEnum enum, the only methods visible to them are the ones declared in the enum itself, and also, of course, any inherited methods from the parent of all enums, java.lang.Enum
.
To make the method visible, it must be declared as a method of the enum itself:
class Scratch {
enum MyEnum {
ONE {
public void test1(String v1, String v2) { }
},
TWO {
public void test2(int v3) { }
};
public void test1(String v1, String v2) { }
public void test2(int v3) { }
}
public static void main(String[] args) {
MyEnum.ONE.test1("a", "b");
MyEnum.ONE.test2(0);
// remember that an enum variable can be re-assigned, and so
// all visible methods should be methods of the MyEnum type
MyEnum foo = MyEnum.ONE;
foo = MyEnum.TWO;
}
}
Remember that an enum variable can be re-assigned, and any enum variable should be able to potentially call the same methods.