overriding static method
Ok let's say I have a first class :
publicclass A{
staticpublicvoid test(){
}
}
Then a second :
publicclass Bextends A{
staticpublicvoid test(){
}
}
Why test() in class B is not considered an override of test in class A ? (the up arrow doesn't appear in eclipse and the @override tag provoque an error).
Thanks.
[969 byte] By [
JF_La] at [2007-11-26 12:28:35]

The simple answer is that static methods are associated with the class itself, not with an instance of the class, so when you call test(), which version gets called depends on what class you call it on,
A.test();
or
B.test();
You can call it on an instance, but again, the type of the reference determines which method is called.
Someone please correct me if I'm wrong.
You can't override static methods, simple as that.kind regards,Jos
My goal was to be sure that every class inherited from class "A" would have to provide a particuliar static method.Is there a common work-around to achieve this ?
JF_La at 2007-7-7 15:37:08 >

I think an abstract method is what you're after, but I'm not sure if static methods can be abstract.
> Is there a common work-around to achieve this ?
Yes, use objects (instances) and abstract methods, if you use (pure) Java.
Otherwise, use a programming language capable of defining your own Metaclasses. There has been an extensible Java derivate called "OpenJava", which I think would have pleased your needs, but the project seems stale for some years. Maybe AspectJ may help out, but I am not sure.
Static methods cannot be abstract, as there is no inheritance involved here. Static methods cannot be overridden, a subclass only can "hide" static methods of its superclasses.
Thanks stefan, that answers all my questions.
JF_La at 2007-7-7 15:37:08 >

> Static methods cannot be abstract, as there is no> inheritance involved here. <pb>Static methods are inherited. But they cannot be overridden, and abstract applies specifically to methods that can be (must be) overridden.</pb>
Hm, yes, sorry for being imprecise here. :)
To me, this is a different kind of inheritance, as you can define a static method having the same signature as one defined by some super class, but it does not override the super one and you actually cannot call some kind of super, especially as you do not have an inheritance hierarchy on static methods in Java.
>and you actually> cannot call some kind of super, especially as you do> not have an inheritance hierarchy on static methods> in Java.There's nothing to stop B.test() from calling A.test(), however.
> There's nothing to stop B.test() from calling
> A.test(), however.
Right. But this is just because you add your knowledge on the hierarchy on the time of creation of B, which might change (e.g. introduce a C with A <- C <- B having a static C.test() as well or make B not extend A anymore).