This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes static final method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static final method" Watch "static final method" New topic
Author

static final method

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

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?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
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

Joined: Dec 14, 1998
Posts: 20494
    ∞

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?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

Thanks Jim. And I agree with you - the whole hiding static methods thing seems like a recipe for headache.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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?



Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.
Chris Montgomery
Ranch Hand

Joined: Jan 14, 2004
Posts: 141
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

Joined: Jan 14, 2004
Posts: 141
wow. I missed the boat. I apologize. I was defining a variable NOT a method and now I have to leave.

Sorry!
Amar Shrivastava
Greenhorn

Joined: Oct 17, 2005
Posts: 13
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static final method
 
Similar Threads
final static int i =0 Whizlabs says i is variable and i feel it is a constant
private constructor
static method inside a abstract class?
static final
Whats the Difference