public class Test {
public static enum MyEnum {
valueA(1),valueb(2),valuec(3),valued(4);
private int i;
private Object o;
private MyEnum(int number) {
i = number;
}
public void set(Object o) {
this.o = o;
}
public Object get() {
return o;
}
}
public static void main(String[] args) {
System.out.println(MyEnum.valueA.i); // private
}
}
output: 1
Why 1 is shown if it a private member in enum?
Outer classes have full access to the member variables of their inner classes, therefore
i is visible in the Test class.
In contrast, if MyEnum was external to the Test class, the compiler would complain about the visibility of i,
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