11

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
  }
}
Kip
  • 107,154
  • 87
  • 232
  • 265

6 Answers6

10

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.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
Sug
  • 822
  • 1
  • 7
  • 9
  • The problem I have with this approach is that it is confusing (or perhaps 'surprising') - I've just spent a while in a code review working out why the developer had imported an enum other than the Enum in the class; they hadn't but that's what it looked like because I wasn't reading the import properly: I thought we'd accidentally either created two identically-named classes in subtly-different packages. At least using MyEnum.DOG removes the need for the import and reduces the potential for confusion. – Rich Nov 28 '22 at 14:18
5

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
  • 257,207
  • 101
  • 511
  • 656
3

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
  • 145,806
  • 30
  • 211
  • 305
  • 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 Jan 12 '10 at 14:42
2

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
  • 38,619
  • 8
  • 86
  • 96
  • 6
    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 Jan 12 '10 at 12:06
  • @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 Jan 12 '10 at 14:41
1

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
  • 29,240
  • 13
  • 74
  • 99
0

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

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 3
    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 Jan 12 '10 at 03:22