This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
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.
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.
Joined: Jan 30, 2000
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.
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.
Joined: Jan 14, 2004
wow. I missed the boat. I apologize. I was defining a variable NOT a method and now I have to leave.
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com