I have been playing around with modifiers with static method and came across a weird behaviour.
As we know, static methods cannot be overridden, as they are associated with class rather than instance.
So if I have the below snippet, it compiles fine
//Snippet 1 - Compiles fine
public class A {
static void ts() {
}
}
class B extends A {
static void ts() {
}
}
But if I include final modifier to static method in class A, then compilation fails ts() in B cannot override ts() in A; overridden method is static final.
Why is this happening when static method cannot be overridden at all?
I think the compilation error was quite misleading here. It should not have said "overridden method is static final.", but it should instead have said "overridden method is final.". The static modifier is irrelevant here.
A static method can't be overridden in Java, unlike non-static methods. But they are inherited like static and non-static data members. That is why a non-static method with the same name can't be created in the parent class
The
final
keyword ensures that the specific method body be run everytime a call to the method. Now if a static method is created in the child class with the same name and a call to the method is made, the method in the subclass gets executed which should not be the case if final is prefixed before the static method name in the parent class. Hence final keyword restricts the creation of method with the same name in the child class.Static methods belong to the class, not the instance.
A.ts()
andB.ts()
are always going to be separate methods.The real problem is that Java lets you call static methods on an instance object. Static methods with the same signature from the parent class are hidden when called from an instance of the subclass. However, you can't override/hide final methods.
You would think the error message would use the word hidden instead of overridden...
Static methods cannot be overridden but they can be hidden. The
ts()
method of B is not overriding(not subject to polymorphism) thets()
of A but it will hide it. If you callts()
in B (NOTA.ts()
orB.ts()
... justts()
), the one of B will be called and not A. Since this is not subjected to polymorphism, the callts()
in A will never be redirected to the one in B.The keyword
final
will disable the method from being hidden. So they cannot be hidden and an attempt to do so will result in a compiler error.Hope this helps.
The ts() method in B is not overriding the ts() method in A, it simply is another method. The B class does not see the ts() method in A since it is static, therefore it can declare its own method called ts().
However, if the method is final, then the compiler will pick up that there is a ts() method in A that should not be overridden in B.
You might find yourself in the position to think about making a static method final, considering the following:
Having the following classes:
Now the 'correct' way to call these methods would be
which would result in
AB
but you could also call the methods on instances:which would result in
AB
as well.Now consider the following:
that would print
A
. That might surprise you since you are actually having an object of classB
. But since you're calling it from a reference of typeA
, it will callA.ts()
. You could printB
with the following code:In both cases the object you have is actually from class
B
. But depending on the pointer that points to the object, you will call method fromA
or fromB
.Now let's say you are the developer of class
A
and you want to allow sub-classing. But you really want methodts()
, whenever called, even from a subclass, that is does what you want it to do and not to be hidden by a subclass version. Then you could make itfinal
and prevent it from being hidden in the subclass. And you can be sure that the following code will call the method from your classA
:Ok, admittetly that is somehow constructed, but it might make sense for some cases.
You should not call static methods on instances but directly on the classes - then you won't have that problem. Also IntelliJ IDEA for example will show you a warning, if you call a static method on an instance and as well if you make a static method final.