javasyntaxenumsstatic-import

Can I do a static import of a private subclass?


I have an enum which is private, not to be exposed outside of the class. Is there anyway I can do a static import of that type, so that I don't have to type the enum type each time? Or is there a better way to write this? Example:

package kip.test;

import static kip.test.Test.MyEnum.*; //compile error

public class Test
{
  private static enum MyEnum { DOG, CAT }

  public static void main (String [] args)
  {
    MyEnum dog = MyEnum.DOG; //this works but I don't want to type "MyEnum"
    MyEnum cat = CAT; //compile error, but this is what I want to do
  }
}

Solution

  • Or is there a better way to write this?

    If your main goals are to reference the items without their qualifying enum identifier, and maintain this list privately, you could scrap the enum type altogether and use ordinary private static constants.