Java: using switch statement with enum under subclass

JavaEnumsSwitch Statement

Java Problem Overview


First I'll state that I'm much more familiar with enums in C# and it seems like enums in java is a quite mess.

As you can see, I'm trying to use a switch statement @ enums in my next example but I always get an error no matter what I'm doing.

The error I receive is:

> The qualified case label SomeClass.AnotherClass.MyEnum.VALUE_A must be replaced with the unqualified enum constant VALUE_A

The thing is I quite understand the error but I can't just write the VALUE_A since the enum is located in another sub-class. Is there a way to solve this problem? And why is it happening in Java?

//Main Class
public class SomeClass {

	//Sub-Class
	public static class AnotherClass {
		public enum MyEnum {
			VALUE_A, VALUE_B
		}    
		public MyEnum myEnum;
	}
	
	public void someMethod() { 
		MyEnum enumExample //...
		
		switch (enumExample) {
			case AnotherClass.MyEnum.VALUE_A: { <-- error on this line
				//..
				break;
			}
		}
	}
}

Java Solutions


Solution 1 - Java

Change it to this:

switch (enumExample) {
    case VALUE_A: {
        //..
        break;
    }
}

The clue is in the error. You don't need to qualify case labels with the enum type, just its value.

Solution 2 - Java

Wrong:

case AnotherClass.MyEnum.VALUE_A

Right:

case VALUE_A:

Solution 3 - Java

Java infers automatically the type of the elements in case, so the labels must be unqualified.

int i;
switch(i) {
   case 5: // <- integer is expected
}
MyEnum e;
switch (e) {
   case VALUE_A: // <- an element of the enumeration is expected
}

Solution 4 - Java

this should do:

//Main Class
public class SomeClass {

    //Sub-Class
    public static class AnotherClass {
        public enum MyEnum {
            VALUE_A, VALUE_B
        }    
        public MyEnum myEnum;
    }

    public void someMethod() { 
        AnotherClass.MyEnum enumExample = AnotherClass.MyEnum.VALUE_A; //...

        switch (enumExample) {
            case VALUE_A: { //<-- error on this line
            //..
            break;
            }
        }
    }
}

Solution 5 - Java

From Java 14 onwards, one can use switch expressions.

For this post

public enum MyEnum {
    VALUE_A, VALUE_B;
}
public void someMethod() { 
	MyEnum enumExample //...

	switch (enumExample) {
		case VALUE_A -> {
			// logic
		}
		case VALUE_B -> {
			// logic
		}	
	}
}

Switch expression

> Like all expressions, switch expressions evaluate to a single value and can be used in statements. They may contain "case L ->" labels that eliminate the need for break statements to prevent fall through. You can use a yield statement to specify the value of a switch expression.

public enum Month {
    JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC;
}

Example 1: Returns value.

public static int getNoOfDaysInAMonth(Month month, boolean isLeapYear) {
	return switch(month) {
		case APR, JUN, SEP, NOV -> 30;
		case FEB -> (isLeapYear)? 29: 28;
		case JAN, MAR, MAY, JUL, AUG, OCT, DEC -> 31;
	};
}

Example 2: Doesn't returns value.

public static void printNoOfDaysInAMonth(Month month, boolean isLeapYear) {
	switch(month) {
		case APR, JUN, SEP, NOV -> {
			System.out.println("30 days");
		}
		case FEB -> {
			System.out.println(((isLeapYear)? 29: 28) + " days");
		}
		case JAN, MAR, MAY, JUL, AUG, OCT, DEC -> {
			System.out.println("31 days");
		}
	};
}

Reference

Switch Expressions

Solution 6 - Java

This is how I am using it. And it is working fantastically -

public enum Button {
        REPORT_ISSUES(0),
        CANCEL_ORDER(1),
        RETURN_ORDER(2);

        private int value;

        Button(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

And the switch-case as shown below

@Override
public void onClick(MyOrderDetailDelgate.Button button, int position) {
    switch (button) {
        case REPORT_ISSUES: {
            break;
        }
        case CANCEL_ORDER: {
            break;
        }
        case RETURN_ORDER: {
            break;
        }
    }
}

Solution 7 - Java

Write someMethod() in this way:

public void someMethod() {

	SomeClass.AnotherClass.MyEnum enumExample = SomeClass.AnotherClass.MyEnum.VALUE_A;

	switch (enumExample) {
	case VALUE_A:
		break;
	}

}

In switch statement you must use the constant name only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPopokokoView Question on Stackoverflow
Solution 1 - JavadarrengormanView Answer on Stackoverflow
Solution 2 - JavaAkash YellappaView Answer on Stackoverflow
Solution 3 - JavaKruView Answer on Stackoverflow
Solution 4 - JavaWoyzeckView Answer on Stackoverflow
Solution 5 - JavahbamithkumaraView Answer on Stackoverflow
Solution 6 - JavaJimit PatelView Answer on Stackoverflow
Solution 7 - Javadash1eView Answer on Stackoverflow