The documentation for the anonymous classes states:
Anonymous classes also have the same restrictions as local classes with respect to their members:
- You cannot declare static initializers or member interfaces in an anonymous class.
- An anonymous class can have static members provided that they are constant variables.
I don't understand how would a static constant in an anonymous class be useful in practice. Static methods can't be overridden and we can't access the anonymous class's members from outside, therefore we don't need to use static constants.
Am I missing something or is it something that's not practical in any way, but it's permitted anyways?
You're missing:
Naming constants provide clarity to the programmers that needs to maintain the code.
Naming a constant allows it to be used in multiple places in the anonymous class code, making it easier to change later without missing a spot.
This is really no different than using private
constants in any other class.