101

I have a class that defines its own enum like this:

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

    public static void aTestMethod() {
        Test2(E1);  // << Gives "E1 cannot be resolved" in eclipse.
    }
    public Test2(MyEnum e) {}
}

If I specify MyEnum.E1 it works fine, but I'd really just like to have it as "E1". Any idea how I can accomplish this, or does it have to be defined in another file for this to work?

CONCLUSION: I hadn't been able to get the syntax for the import correct. Since several answers suggested this was possible, I'm going to select the one that gave me the syntax I needed and upvote the others.

By the way, a REALLY STRANGE part of this (before I got the static import to work), a switch statement I'd written that used the enum did not allow the enum to be prefixed by its type--all the rest of the code required it. Hurt my head.

3
  • 11
    Regarding the "really strange" behavior on the switch statements: Java does this on all enums in a switch statement. Since you know what type you're switching on, they figure there's no reason to make you specify the type again in every case block. download.oracle.com/javase/tutorial/java/javaOO/enum.html Commented Sep 29, 2011 at 23:56
  • 1
    I guess what got to me is that it would not allow the Enum type prefix. Everywhere else prefixing is either required (if not imported) or optional. Commented Apr 4, 2014 at 16:25
  • 1
    I agree with you in reference to the switch inconsistency, makes less experienced Java programmers scratch their heads. Commented Sep 23, 2015 at 14:07

3 Answers 3

157

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) {}
}
Sign up to request clarification or add additional context in comments.

11 Comments

I alluded to this as a solution in my question, but I'd really rather not do that. The enums are only used inside this one file. It just seems strange that this doesn't just automatically work.
@Bill K, why do you think it should automatically work? How would you expect this to work then? class A { enum B {X,Y}; enum C {X,Z}; }
This solution works only because MyEnum is visible outside of Test. Solution will not work if MyEnum is, e.g., private.
Do not forget the last part, symbols ".*" in "import static mypackage.Test.MyEnum.*;" as I did!
The word nested should be underlined. Funny that you can not static import a non nested enum. You can do it in Groovy, but in my Eclipse and IntelliJ IDEs I have the same problem as above: no autocompletion for enums, not even if I use the enum type as in TYPE.VA...
|
18

You can do a static import on a nested class:

import static apackage.Test.Enum.*;

Comments

11

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.*;

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.