File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final static methods vs non-final static methods

 
Sarah Rising
Ranch Hand
Posts: 38
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no difference. Static methods are implicitly final, and adding the keyword just makes it explicit.
 
Sarah Rising
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much Stephan.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic