views:

92

answers:

6

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
  }
}
+2  A: 

Nope, that's pretty much what private is all about.

OscarRyz
On the other hand, the outer class can access all private fields and method directly. So the case could be made for wanting to import it.
Thilo
+1  A: 

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.

Thilo
A: 

You could simply write your code inside the enum itself.

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

The other place where private enums can be references without their class is in a switch statement:

private static enum MyEnum {
    DOG, CAT
}

public static void main(String[] args) {
    MyEnum e = null;
    switch (e) {
    case DOG:
    case CAT:
    }
}
brianegge
+3  A: 

You can use the no-modifier access level, i.e.

static enum MyEnum { DOG, CAT }

MyEnum will not be visible to classes from other packages neither for any subclass, it's the closest form of private, yet letting you avoid explicitly referencing MyEnum

Sug
+2  A: 

It may (or may not) be reasonable to move some of the code into (static) methods of the enum.

If pressed, you could duplicate the static fields in the outer class.

private static final MyEnum CAT = MyEnum.CAT;
private static final MyEnum DOG = MyEnum.DOG;

Icky, but a possibility.

Tom Hawtin - tackline
Yeah I thought of this too. I guess it's either this or just using the qualified name. Neither is ideal, but I think using the qualified name will be less error-prone at least.
Kip
+4  A: 

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.

akf
no, enum provides other forms of safety other than just encapsulation - for example, you can use them in switch statements, and you can get warnings if you omit one. Just stick with the qualifier - there are worse verbosity problems in java ;)
Chii
@akf: Chii's right, in my actual code the enums have some behavior beyond what can be trivially done with static constants. I just simplified all that out for the question so it would be the bare minimum example. I guess I'm just stuck with long qualifier names (much longer than "`MyEnum`" in my actual code unfortunately...)
Kip
@Kip: in this case, why did you accept this answer?
Thilo