static keyword is not redundant. You can create a static nested class (with the static ) or an inner class (without it). In the first case, the class will not be tied to any specific enumeration value. In the second case, instances of the inner class must have a private instance - one of the enum values:
public class Test { public static void main(String[] args) { MyEnum.VALUE_1.createInnerObject().showName(); MyEnum.VALUE_2.createInnerObject().showName(); } public enum MyEnum { VALUE_1, VALUE_2; public MyInnerClass createInnerObject() { return new MyInnerClass(); } private class MyInnerClass { public void showName() { System.out.println("Inner class assigned to " + MyEnum.this + " instance"); } } } }
In the above example, you cannot create an instance of MyInnerClass directly from MyEnum :
new MyEnum.MyInnerClass();
To do this, you need to have a static nested class, but then you cannot use something like MyEnum.this .
Tomek Rękawek
source share