Please consider the following code sample:
public enum MyEnum {
FIRST {
@Override
public void someMethod() {
...
}
},
SECOND {
@Override
public void someMethod() {
...
}
public void someOtherMethod() {
...
}
};
public abstract void someMethod();
}
Is it possible to call someOtherMethod()? I tried MyEnum.SECOND.someOtherMethod() but the IDE could not resolve it.
Thanks in advance...
MyEnum.SECOND.someOtherMethod() is illegal because of this rule pertaining to the class bodies on enum constants:
Instance methods declared in these class bodies may be invoked outside the enclosing enum type only if they override accessible methods in the enclosing enum type. [JLS §8.9.1]
So since someOtherMethod() doesn't override a MyEnum method, you can't invoke it outside of MyEnum. You could, however, invoke it somewhere in the body of SECOND, and you might even be able to invoke it from the body of one of the other enum constants like FIRST, although I haven't tried it and frankly that would be a bit weird.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With