-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Cannot use final field of enum in constant expression. #50166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is expected behavior. The |
But it comes from the constant class. And it is a final. |
Making final fields of "constant classes" (classes with a constant constructor) be readable as constants when used in an otherwise constant expression looks reasonable. The problem is that it breaks getter/field symmetry. The author of the class didn't explicitly choose to make that field usable in constant expressions, which means that they might choose to change the field to a getter in a later version of the same class. (Or in a subclass, but at least that does not affect enums, not unless we choose to allow enum values to add or override methods. That has been requested, and Java allows it for similar enums.) If the author changes the field to a getter, then the code expecting to use the field as a constant will break. But changing a field to a getter should be a safe refactoring, that's one of the points of having getters/setters that act precisely like fields to begin with. And avoiding the private-field/public-getter-setter of Java was another goal. Now, if the class author could declare the field explicitly |
I am going to close this - given that the question is answer and improvements are tracked in a separate issue. |
Uh oh!
There was an error while loading. Please reload this page.
I am trying to use enhanced enums, inside the switch statement, interestingly enough I get an following error when using one of the final fields of the enum to check in switch statement:
My enum MsgType looks like the following:
Error happens when checking for the name field, in switch statement.
The text was updated successfully, but these errors were encountered: