3

I defined an enum type that implements an interface as follows:

public enum MyEnum implements MyInterface
{
    val1, val2, val3;
    private MyEnum() {}
    private MyEnum(Parcel in)
    {
        readFromParcel(in);
    }

    public void readFromParcel(Parcel in)
    {
        MyEnum val = MyEnum.values()[in.readInt()];
        // ??? How to I assign val to my current enum?
    }

}

How do I access the value of the current enum object so I can make the assignment inside of readFromParcel() ? (Please see comment in code)

zer0stimulus
  • 22,306
  • 30
  • 110
  • 141

4 Answers4

14

Inside an instance method, you can refer to the "current enum object" as simply this. It works this way because enum constants are actual objects, i.e. instances of a class -- a very special type of class, but a class nonetheless. (Note that I mentioned that these are enum constants: it is the convention in Java to use all uppercase letters when naming constants.)

Your usage of values() is also very peculiar (not to mention that it'll perform horribly since a new array must be constructed at each call). Perhaps you'd want to take a look at EnumMap, which is a special kind of Map optimized for enum constants as keys.

If you're trying to mutate fields contained in these enum constants, then you should seriously consider a redesign. You should generally minimize mutability anyway, but having these static singletons be mutable does not sound like a good design. Instead of having these mutable fields intrinsic within the enum constants themselves, a Map from the constants to these mutable values would be a much better design.

See also

  • Java Tutorials/enum
  • Effective Java 2nd Edition
    • Item 15: Minimize mutability
    • Item 31: Use instance fields instead of ordinals
    • Item 32: Use EnumSet instead of bit fields
    • Item 33: Use EnumMap instead of ordinal indexing

Various questions on Java enum

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1

Enums are immutable, so you cannot. Your best be is to make readFromParcel static and have it return an enum. So, something like this:

    public static MyEnum readFromParcel(Parcel in)
    {
        MyEnum val = MyEnum.values()[in.readInt()];
        return val;
    }
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
  • 3
    They aren't necessarily immutable; they just should be. – Tom Hawtin - tackline Jul 26 '10 at 13:55
  • 1
    Enums are immutable, the ordinal and name fields are final. You can't have an instance of an enum read a value and become a different instance. It just won't work. Now, if you add additional data, that can be mutable. But mutable data for an enum means you're doing something wrong. – Devon_C_Miller Jul 26 '10 at 15:36
0

It's really not a good idea to make enums mutable. However, the this for an enum is just this.

MyEnum val = this;

The actual fields val1, val2 and val3 (should be VAL1, VAL2 and VAL3) are implicitly public static final.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

Use this.

if this.equals(val) {
     ...
}
Adrian Regan
  • 2,240
  • 13
  • 11