Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

public static final method modifier

 
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel like I understand all usages of final in Java.

This one corner case I am not sure of.

final as a modifier on a static method definition

public static final <T> List<T> emptyList()

We normally say that "static methods are not inherited" because they can only be called using the Classname.methodName() syntax.

I know what effect it has:



So the meaning is "Do not allow a subclass to create a static method with the same signature??"

I know it isn't used very often, not sure why it gets used at all.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread I noticed only after posting suggests that it prevents HIDING the static method, and calls out the subtle distinction between saying:

"A subclass can NOT override a static method of a superclass" <-- true

"A subclass does not inherit static methods from superclasses" <-- commonly said but not strictly true

We can see it is not true that it isn't inherited, because it turns out that any static method (at least any public or protected static method) in the base class can actually be referenced without the Classname.methodName(), i.e. just as methodName().  Just because I've gone like my whole life without doing that, didn't mean it wasn't legal.  Phooey.

 
Marshal
Posts: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you say a static method isn't inherited by its subclass? They are inherited just as non‑private instance methods might be.

Bad case of belt'n'braces, I am afraid. That looks like an escape from the Collections class, which I referred to earlier today. Collections is a utility class, and that doesn't have any accessible constructors. So it is impossible for us to create subtypes of. I don't know of any subtypes in its own package, if it has a package‑private constructor. Your example has two utility classes but with default constructors, so subtyping is, in theory, possible.
So yes, the final modifier is there to prevent any subtypes hiding/redefining that method, but there aren't any such subtypes.

Yes, you might want a static method which cannot be redefined in a subtype. The solution to that problem is always to call static members on their class name.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, you might want a static method which cannot be redefined in a subtype. The solution to that problem is always to call static members on their class name.



I do that so dang often (okay, all the time) that I forgot you don't need to prefix with the class name to refer to them within code in a subclass.

Bad case of belt'n'braces, I am afraid.


If you explain that phrase I will have learned two things from this thread, one about Java and one about...colorful English phrases.

I am not familiar with it at all.

Okay, I had no idea this was a Software Engineering term (never heard anyone say it aloud in the States, ever).

Wherein punters argue over the merits of "Belt and Braces" in validations:
https://stackoverflow.com/questions/767952/is-belt-and-braces-programming-good-practice-or-just-introducing-needless-comple



 
Sheriff
Posts: 26947
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:

Bad case of belt'n'braces, I am afraid.


If you explain that phrase I will have learned two things from this thread, one about Java and one about...colorful English phrases.

I am not familiar with it at all.

Okay, I had no idea this was a Software Engineering term (never heard anyone say it aloud in the States, ever).



I'd expect that "belt and suspenders" is the North American equivalent.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Holy Moose, Batman!"

I collect phrases like hoarders collect junk and cats, and I never knew this one in North American nor UK English.

It has Great Precedent, I see it in a book about ALGOL 68:
https://www.google.com/books/edition/Programming_and_Problem_Solving_in_Algol/1jBdDwAAQBAJ?hl=en&gbpv=1&dq=%22belt+and+braces%22+programming&pg=PA112&printsec=frontcover

It is also a proper noun:
https://www.largestcompanies.com/company/Belt-and-Braces-Software-AB-3479791

It was in use for old C++ texts:
https://www.google.com/books/edition/Math_Toolkit_for_Real_Time_Programming/rjFRDwAAQBAJ?hl=en&gbpv=1&dq=%22belt+and+suspenders%22+programming&pg=PA28&printsec=frontcover

Again, I ask a dumb question and learn unrelated stuff I didn't realize I didn't know.

Thanks, Moose Crew.
 
Campbell Ritchie
Marshal
Posts: 74627
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . you don't need to prefix with the class name to refer to them within code in a subclass.

Not until you redefine the method and hide its original. Then missing out the class name can cause you no end of confusion.

. . . If you explain that phrase . . .

Two different ways to stop your trousers falling down. If you have the one, do you need the other? Dictionary link.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic