So I have looked around google and SO , I cannot find a example or explanation of : What is the purpose of static
final
methods in enum
?
What I understand :
static
can be accessed like function/procedural languages.final
means you can't override it. Can't change the reference. As assylias pointed out in comments static
can't be overridden either.enum
s can't be subclassed, explained here.So what's the point of static
final
methods in enum
s if it will never be overridden since there won't be a subclass?
By making a static method final, you prevent it from being hidden (only instance methods can be overriden) by a subclass.
Since enum can't be subclassed, making a static method final is superfluous but not forbidden.
Note: technically, each enum constant that has a class body implicitly defines an anonymous class that extends the enum. But since inner classes may not declare static methods, the static final method could not be hidden in such a constant's class body.
It's easy to see why static
methods make sense, so I guess the question is about the final
modifier.
final
serves no purpose here, except maybe make the code a bit easier to read, but not by much.
It's similar to the way interface
methods are often written as public void foo();
, even though interface members are always public anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With