• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

significance of private with final modifier method

 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Hunter
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic