I'm wondering why this is okay:
class SomeClass {
//--snip--
private final void doStuff()
{
// private work here
}
}
If it's private, there's no way anyone can override it, right?
Why is it possible to add final
keyword if it has no effect? (or am I missing something?)
Basically, it's allowed because they didn't feel like it's worthwhile to put a special case prohibiting the
private
modifier. It's like how you can also declare methods on an interface aspublic
, or nested classes in an interface asstatic
, even though those keywords are implied in interfaces. You can also declarefinal
methods on afinal
class, etc.Java took the stance of not complaining when you add redundant modifiers. They do it consistently.
One edge case that requires a private method to be final is when the SafeVarargs annotation is used. The following code does not compile, because the private method is not final.
It makes the language more flexible but the language does not guarantee that it will have any effect. Making a private method final is a hint to the (JIT) compiler.
The Java Language Specification notes that:
From Wikipedia: