Search code examples
javaoopmethodsfinal

Why private method can not be final as well?


Is it redundant to add private and final to a same method?

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?)


Solution

  • 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 as public, or nested classes in an interface as static, even though those keywords are implied in interfaces. You can also declare final methods on a final class, etc.

    Java took the stance of not complaining when you add redundant modifiers. They do it consistently.