2

I have a Java class with a 2-option enum inside of it. The outer class has a member instance of the enum. I have a method that switches the member's option. Like so:

    public class Foo {
        public enum BarEnum {
            OPTION1, OPTION2;
        }

        private BarEnum barmember;

        private void switchBarmember {
            switch (barmember) {
                case OPTION1: barmember = BarEnum.OPTION2; break;
                case OPTION2: barmember = BarEnum.OPTION1; break;

            }
        }
    }

My question is, is there a way to execute the change without saying BarEnum.? In other words, is there a way to make the method look like this:

        private void switchBarmember {
            switch (barmember) {
                case OPTION1: barmember = OPTION2; break;
                case OPTION2: barmember = OPTION1; break;

            }
        }

If there isn't a way, just let me know. Thanks!

LastStar007
  • 725
  • 1
  • 8
  • 21

3 Answers3

2

Not exactly, but this will work:

public class Foo {
    public enum BarEnum {
        OPTION1,
        OPTION2;

        private BarEnum switchValue ( )
        {
            switch( this )
            {
            case OPTION1:
                 return OPTION2;
            case OPTION2:
                 return OPTION1;
            }

            throw new AssertionError("Should not be here");
        }
    }

    private BarEnum barmember;

    private void switchBarmember {
        barmember = barmember.switchValue( );
    }
}
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
1

Maybe this way :)

class Foo {
    public enum BarEnum {
        OPTION1, OPTION2;
    }
    private BarEnum OPTION1=BarEnum.OPTION1;
    private BarEnum OPTION2=BarEnum.OPTION2;

    private BarEnum barmember;

    private void switchBarmember() {
        switch (barmember) {
            case OPTION1: barmember = OPTION1; break;
            case OPTION2: barmember = OPTION2; break;

        }
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • That kind of defeats the purpose of having encapsulated constants, but it's good to see someone who is willing to break the rules in order to be successful. Here's some free rep. – LastStar007 Jul 10 '12 at 21:56
  • @LastStar007 thanks, at last I gain 200 rep one day :D now I can go sleep – Pshemo Jul 10 '12 at 21:58
1

This is not possible unless you create new fields like @Pshemo did.

If this enum was in a separate file, it would be possible to statically import the enum constants to achieve this:

import static foo.TestEnum.ONE;
import foo.TestEnum;

public class StaticImportEnum {
    public static void main (String[] args) {
        TestEnum foo = ONE;
    }
}


package foo;

public enum TestEnum {
    ONE, TWO;
}

However, you should use static imports sparingly as they are generally considered bad practice.

Community
  • 1
  • 1
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • I thought about static imports but I didn't want to overthrow my existing file hierarchy. Besides, in this case it would violate the PLA. IMHO, static imports are just a tool, not good or bad, but they are easily abused and that is why they are generally discouraged. Thanks for your time. Here's some free rep. – LastStar007 Jul 10 '12 at 21:55
  • Static import works for nested enums too, as demonstrated in [this answer](http://stackoverflow.com/a/1677074/822870) – David Balažic Sep 09 '15 at 01:07