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
}
}
Considering that you can access the field fully qualified, I would say that it is a bug in the compiler (or language spec) that you cannot statically import it.
I suggest that you make the enumeration package-protected.
You can use the no-modifier access level, i.e.
enum MyEnum { DOG, CAT }
MyEnum
will not be visible to classes from other packages neither from any subclass. It is the closest form of private, yet letting you avoid explicitly referencing MyEnum
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With