Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final static methods vs non-final static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final static methods vs non-final static methods" Watch "final static methods vs non-final static methods" New topic
Author

final static methods vs non-final static methods

Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
Hello, all.

Does anybody know if there is a significance to what the final means in a final static method?

What would be the functional significance to the difference between a final static method and a non-final static method?

Thanks in advance for your thoughts.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3594
    
  14

There's no difference. Static methods are implicitly final, and adding the keyword just makes it explicit.
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
Thanks much Stephan.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

There is a slight difference. You can't override a static method, but you can hide it in a subclass. Adding final prevents this.

I wasn't sure about this, but I've just tested it. And the Java Language Specification says:
A method can be declared final to prevent subclasses from overriding or hiding it.
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
Thanks, Matthew.

1) I've always been unclear just what the functional significance of hiding actually means, especially relative to the term override. (Yes, I know fields can't be overridden, nor can static methods.)

Question 1: Does "hiding" imply that choosing between two members with the same name (fields or methods) -- one in a superclass, the other in a subclass -- is done at compile time as opposed to execution time?

Question 2: Is there a technical definition of the term "hide?"


Edit:
2) It is interesting that if:
- you have two static methods -- one in a superclass, and one in the subclass -- that conform to the rules for overriding methods
- except for that both are static

then if you make the superclass method final, Netbeans will give an editor message that you are trying to override two methods but can't because they're static.

Question 3: Does that make any sense to anybody.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

I think you've got it, but here's an example that I hope will make it clear.

With the last method commented out, that will print "Superclass version" (proof that static methods are inherited). Now uncomment it, and it will print "Subclass version". However, you can still call the base-class version using Superclass.doSomething() - which shows that it hasn't been overridden. Instead, it's just been "hidden" - it can no longer be called without the qualification. And, as you suggested, this is all worked out at compile time.

Now add final to the base-class method, and you'll get a compiler error.

Does that help?
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
Thanks, again Matthew,

Hmmmm....

I was under the impression that the term "inherit" had an implication of an instance of an object and not applying specifically to the class as static members do.

Question 4: So, is it technically correct to say that a subclass (Subclass in your code) actually "inherits" the static method doSomething() from Superclass? Or is main() using it as part of the class, and when it's uncommented in Subclass, the compiler is hiding the doSomething() method in Superclass and choosing the method in Subclass?

This might be finicky with the emphasis on terminology, but I think it's probably fairly important in getting all this stuff completely understood.

Question 5: Anybody know if there is a technical definition of "hiding?"

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Yeah, "inherit" may not be the best term there, but I think you can justify it on the grounds that the subclass has access to the method in the subclass purely because it is a subclass.

In terms of the definitive definition of "hiding" applied to Java, your best bet is probably the JLS.
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
I was just looking at 8.4.8. Uhhhh...

As for the term "inherit," I have this from the source I'm using (Programmer's Guide to Java Certification: A Comprehensive Guide):

"Inheritance of members is closely tied to their declared accessibility. If a superclass member is accessible by its simple name in the subclass (without the use of any extra syntax like super), tha t member is considered inherited. This means that private, overridden, and hidden members of the superclass are not inherited."

From just that, it looks like the static doSomething() can be called "inherited," but I think there's some other considerations. I'll look more at 8.4.8 and report back to headquarters. Might take me a while, though.

Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
I believe I misspoke at 2:03 pm. when I thought a static method can't be inherited. From JSL 8.4.3:

"A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (§8.4.8.1) nor hidden (§8.4.8.2) by a declaration in the class."

Doesn't say anything about the method being static. But it is true that a static method is not overridden by a subclass method; it is hidden (as long as the hiding method satisfies the rules for overriding the superclass method). Hence, Matthew's use of the word "inherit" at 1:45 pm seems to be correct.

(I'm assuming everybody gets the same time stamps. (I'm U.S., EST.) If not, this could be confusing.)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Matthew Brown wrote:There is a slight difference. You can't override a static method, but you can hide it in a subclass. Adding final prevents this.

I wasn't sure about this, but I've just tested it. And the Java Language Specification says:
A method can be declared final to prevent subclasses from overriding or hiding it.


Thanks Matthew,I might be unaware from this hidden island but now i am not thanks again
 
Don't get me started about those stupid light bulbs.
 
subject: final static methods vs non-final static methods
 
Similar Threads
strictfp
final keyword for method parameters
static and final methods
Static Method/Non-Static Method, RTTI
Static Hiding