Is it possible use a class name in java switch/case statement? [duplicate]
Asked Answered
G

3

19

I would like to use a java switch statement, which uses class names as case constants. Is it possible somehow? Or do I have to duplicate the class names?

Following code does not work because of compiler error:

case expressions must be constant expressions

String tableName = "MyClass1";

...

switch (tableName) {
case MyClass1.class.getSimpleName():
    return 1;
case MyClass2.class.getSimpleName():
    return 2;
default:
    return Integer.MAX_VALUE;
}

Here is a online demonstration of the issue (openjdk 1.8.0_45): http://goo.gl/KvsR6u

Galliwasp answered 5/1, 2016 at 13:36 Comment(5)
Can you try assigning the strings to final String local variables?Rack
Which java version ? You will get different results on Java 7/8.Gobble
@Gobble I'm using Java 8, but interested in a solution for either version...Galliwasp
Related: https://mcmap.net/q/607696/-can-i-get-a-class-39-s-name-as-a-compile-time-constant-without-hardcoding-it-in-a-string-literal/1857897Galliwasp
Nice explanation of compile-time constant expressions: https://mcmap.net/q/117901/-java-switch-statement-constant-expression-required-but-it-is-constantGalliwasp
B
12

The compiler error already says it. The case labels must be constant expressions and neither, class literals nor the result of invoking getSimpleName() on them, are constant expressions.

A working solution would be:

String tableName = "MyClass1";
...
switch (tableName) {
    case "MyClass1":
        return 1;
    case "MyClass2":
        return 2;
    default:
        return Integer.MAX_VALUE;
}

The expression MyClass1.class.getSimpleName() is not simpler than "MyClass1", but, of course, there won’t be any compile-time check whether the names match existing classes and refactoring tools or obfuscators don’t notice the relationship between the class MyClass1 and the string literal "MyClass1".

There is no solution to that. The only thing you can do to reduce the problem, is to declare the keys within the associated class to document a relationship, e.g.

class MyClass1 {
    static final String IDENTIFIER = "MyClass1";
    ...
}
class MyClass2 {
    static final String IDENTIFIER = "MyClass2";
    ...
}
...
String tableName = MyClass1.IDENTIFIER;
...
switch (tableName) {
    case MyClass1.IDENTIFIER:
        return 1;
    case MyClass2.IDENTIFIER:
        return 2;
    default:
        return Integer.MAX_VALUE;
}

This documents the relationship to the reader, but tools still won’t ensure that the actual string contents matches the class name. However, depending on what you want to achieve, it might become irrelevant now, whether the string contents matches the class name…

Bilow answered 5/1, 2016 at 15:59 Comment(6)
This the correct answer for me: The case labels must be constant expressions and neither, class literals nor the result of invoking getSimpleName() on them, are constant expressions.Galliwasp
Nice explanation of compile-time constant expressions can be found here: https://mcmap.net/q/117901/-java-switch-statement-constant-expression-required-but-it-is-constantGalliwasp
I thought that class literals are constant expressions, but then ""+MyClass1.class should work, but it doesn't...Galliwasp
What is even more counter-intuitive is that enum constant references are not compile-time constants. They can be used in case labels and annotations though, but that's the exception to the usage rules (you can also use class literals in annotations despite not being compile-time constants). So when you say final EnumType X = EnumType.FOO;, then X will not be a compile-time constant and can't be used as case label (unlike primitive types and Strings). The bottom line is that only primitive types and Strings can be compile-time constants (because the specification says so).Bilow
@Bilow - The limitation of this solution is that you cannot easily see which code uses a particular class. You have to use a "find text" option in your IDE instead of "find code which uses that class" option. This can be a problem when you want to refactor all the users of that class.Starlight
@MasterJoe2 in case of Eclipse, it will identify, e.g. MyClass2.IDENTIFIER as a usage of MyClass2. It doesn't work if you don't have the source code, but still isn't a naive text search. If you're referring to the first variant, the limitations have been described in the answer already.Bilow
R
8

Instead of using a switch, why not store the mappings in a map?

Create a map of String to Integer, and map all the class names to their return value.

On a request, if the entry does not exist, return the default value. Otherwise, return the value in the map.

Rowan answered 5/1, 2016 at 13:54 Comment(4)
I was thinking about this solution, but wouldn't be switch more efficient?Galliwasp
Perhaps, but your code will also be less readable as you need a line for every class name and then a few more for every case. The few nanoseconds of optimization you get will likely never be worth the cleaness of the code you can have using a MapRowan
Oh, I forgot to add. If I remember correctly, switches on Strings just switch on their hashcode unless something has changed recentlyRowan
Thanks for your comments +1, but I'm still curious if the switch could work for me..Galliwasp
S
3

Instead of Switch..case why don't you use If..Else. Should work in all versions of java till i know.

if (tableName.equals(MyClass1.class.getSimpleName())) {
     return 1;
} else if (tableName.equals(MyClass2.class.getSimpleName())) {
     return 2;
} else {
     return Integer.MAX_VALUE;
}
Sulphuryl answered 5/1, 2016 at 14:8 Comment(5)
I really do not like else if statements, see the Map based solution by @Sam-SunGalliwasp
is up to the choice, but till i know this is supported in all versions and you will not require to handle any version dependencies.Sulphuryl
the problem of this solution is that in the worst case, it has to go over every single possibility, that's why people want "switch".Low
As far as I can remember the switch case in java is just sugar for if/then/else and the compiled code is just that.Picasso
@Picasso That only applies for switches with few cases. With many cases, a jump table is used.Floyd

© 2022 - 2024 — McMap. All rights reserved.