Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static  Hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static  Hiding" Watch "Static  Hiding" New topic
Author

Static Hiding

vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
In JLS 8.4.6.2 it is given If a class declares a static method ,then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and the superinterfaces of the class that would otherwise be accessible to the code in the class.
The interface methods are implicitly abstract.static modifier cannot be applied to abstract methods.If this is the case, how come a static method in a class can hide a method with same signature in the superinterface?
Thanks,
vinu.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Excellent point! I can't think of a situation where it's possible for a static method to hide a method with the same signature in the superinterface. When I try, the compiler error is:
MyClass.java:14: m1() in MyClass cannot implement m1() in SuperInterface; overriding method is static

If anyone can figure how the JLS is right about this, I'll be much obliged. If not, I'll chalk it up to a careless oversight in the JLS.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I believe you're correct: there is no way for a static method to legally hide a method of the same signature in a superinterface. However this isn't a problem with the JLS. Consider the fuller context from JLS2 8.4.6.2 (or JLS3 8.4.8.2 which says basically the same thing):
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.

The first sentence defines the term hide - that doesn't necessarily imply that hiding is always legal. The second sentence specifies a particular circumstance in which hiding is not legal. All interface methods are instance methods, and so when it says a static method can't hid an instance method, that eliminates hiding of any interface methods. If they hadn't included interfaces in the first sentence, then the second sentence would not have applied, and we'd be left wondering whether or not it's legal to declare a static method with the same signature as a superinterface method.
[ May 01, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi for interfaces

methods----->abstract and instance method(non-static)
variables--->Final and Static.

am i right
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Originally posted by Parameswaran Thangavel:
hi for interfaces

methods----->abstract and instance method(non-static)
variables--->Final and Static.


Fields/variables declared in an interface are implicitly public, static and final, and methods are implicitly public and abstract.

From JLS2 9.3 and 9.4:


9.3
Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

9.4
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public.

[ May 02, 2005: Message edited by: Joyce Lee ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static Hiding