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!

3 Answers 3

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( );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@maasg. It's amazing, but in most Java enum examples it is used as dumb enum from C++. It's time to realize that Java enum is a full-fledged object!
Thanks! I completely overlooked putting methods inside enums.
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;

        }
    }
}

2 Comments

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 thanks, at last I gain 200 rep one day :D now I can go sleep
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.

2 Comments

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.
Static import works for nested enums too, as demonstrated in this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.