How can I reference my Java Enum without specifying its type

Solution 1:

Actually, you can do a static import of a nested enum. The code below compiles fine:

package mypackage;

import static mypackage.Test.MyEnum.*;

public class Test
{
    enum MyEnum{E1, E2};

    public static void aTestMethod() {
        Test2(E1);  
    }

    public static void Test2(MyEnum e) {}
}

Solution 2:

You can do a static import on a nested class:

import static apackage.Test.Enum.*;

Solution 3:

The Test class has to be defined in a package to be importable.

With package defined in Test (IT WORKS):

package mypackage;

You can use:

import static mypackage.Test.MyEnum.*;

Without package defined, you cannot use (DOES NOT WORK):

import static Test.MyEnum.*;