my dog learned polymorphism
The moose likes Beginning Java and the fly likes significance of private with final modifier method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "significance of private with final modifier method" Watch "significance of private with final modifier method" New topic

significance of private with final modifier method

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108

Hi ranchers,

A private method in a class is only visible inside the declaration class and cannot be overridden in the subclass.

Then what is the significance of having a private method with a final modifier? As far as I know there is no significance. If there is any specific significance kindly let me know.

Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

There is no special significance, and indeed, because methods that are private are only visible to the class itself and are not inherited by subclasses, it doesn't make much sense to make private methods final.

Note that for instance variables it's different, because the meaning of the word "final" is different: it means the value can't be changed once it's initialized (it doesn't have anything to do with inheritance).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Note that inner classes can see eachother's privates, and those of the containing class. Therefore you could cook up a scenario where an inner class (which was also a subclass of another inner class) was blocked from overriding a final private method. That's awfully far-fetched, though, and I think "there's no purpose for it" is a fine answer in general.

[Jess in Action][AskingGoodQuestions]
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I just finished a project that has your exact example Ernest, except with an attribute and a class
The outer class is called SymbolTable and the inner nested class is called TableEntry, and the symbol table is made up of entries. SymbolTable only has one attribute which is a private final HashMap. And the only way I wanted the HashMap to be modified was from the SymbolTable class

Shortened Code Below:

Hope this helps,

"If the facts don't fit the theory, get new facts" --Albert Einstein
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

Hunter, your example has a private final inner class TableEntry and a private final instance variable table but there is no private final method, which was what the original question was about.
I agree. Here's the link:
subject: significance of private with final modifier method
It's not a secret anymore!