Java – can I statically import private subclasses?

I have a private enumeration. Don't expose it outside class Anyway, can I statically import this type so that I don't have to enter an enumeration type every 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

If your primary goal is to reference items that do not have qualified enumeration identifiers and maintain this list privately, you can completely discard enumeration types and use ordinary private static constants

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>