• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static final method

 
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just found one of these.

What's it mean?

A final method means that you don't want child classes to override it. But if it's static, it cannot be overridden anyway.

So why does the java language permit static final methods?
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/javaOO/override.html
look at the table at the bottom of the page.

I think it says you can override a static method with another static method
and marking it final would kee you from do this? or would make it impossible to hide the method (as the table seems to imply that you can hide static methods in superclasses).


-Tad
[ October 21, 2005: Message edited by: Tad Dicks ]
 
paul wheaton
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would make sense. I suppose I could fire up the compiler and test this idea, but I'm just too damn lazy. Anybody know fer sure?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using JLS terminology: a static method can never be overridden, but it can be hidden - unless it is final. If you attempt to hide a static final method, that's a compile-time error. Unfortunately, the text of the error you get from Sun's compilers misleadingly refers to overriding rather than hiding. But whether you call it overriding or hiding, you can't do it if the method is final.

Personally I'd like it if all static methods were implicitly final, because I don't think I've ever seen a good reason to hide a static method. IMO it just creates potential for confusion. If you want to define some new static behavior in your subclass, just give the method a new name. That's my two cents, anyway.
 
paul wheaton
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. And I agree with you - the whole hiding static methods thing seems like a recipe for headache.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you hide a static method? I suppose that if you called a static method on an instance, you could hide them that way, but you shouldn't call static methods that way anyway...

And the static methods are bound at compile time, so that the method called is based on the reference type, not that actual type. So hiding a static method is pointless, 'cause what can you hide it from?

 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you hide a static method?

Ummm... by looking at the JLS definition of hiding? I'm not sure what your intended point here is. Are you disagreeing with what one of us said? I think Paul and I agree with you that "hiding a static method is pointless". (At best.) Beyond that I'm not sure who you're addressing your comments to.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Believe me, this is a tuff concept to ingest...

A static reference has more to do with *how* you access it than what you can do with the value it possesses. You don't need to instantiate the class. You call the class directly and you get the value the class has stored. Everyone sees it. Done.

When you deal with an instance of a class, you have your own private class you can change however you want and no one else is bothered. In other words, knock yourself out, no one else cares.

With a static variable, you are completely able to modify that value, but know that when you do, EVERYONE sees the change. Now you have to be careful.


Lastly, a final variable is final. No one can change it. It is what it is... It's non-negotiable. It is the absolute Truth.
 
Chris Montgomery
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow. I missed the boat. I apologize. I was defining a variable NOT a method and now I have to leave.

Sorry!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A static mehtod cannot override an inheritted instance method but it can hide a static method. This is because static methods are class specific and overriding is associated with objects not class(i.e. only instance methods are overridden).

Overridden method calls are resolved at runtime depending on the type of object being referred at runtime but binding of method call to method implementation is done at compile time if the method is static or final.

So defining a method as static final helps to call the method without instanciating the class(i.e. because it is static) and also prevents changing its implementation(i.e. because it is final).
 
reply
    Bookmark Topic Watch Topic
  • New Topic