I understand that in this code:
class Foo {
public static void method() {
System.out.println("in Foo");
}
}
class Bar extends Foo {
public static void method() {
System.out.println("in Bar");
}
}
.. the static method in Bar ‘hides’ the static method declared in Foo, as opposed to overriding it in the polymorphism sense.
class Test {
public static void main(String[] args) {
Foo.method();
Bar.method();
}
}
…will output:
in Foo
in Bar
Re-defining method() as final in Foo will disable the ability for Bar to hide it, and re-running main() will output:
in Foo
in Foo
(Edit: Compilation fails when you mark the method as final, and only runs again when I remove Bar.method())
Is it considered bad practice to declare static methods as final, if it stops subclasses from intentionally or inadvertantly re-defining the method?
(this is a good explanation of what the behaviour of using final is..)
I don’t consider it’s bad practice to mark a
staticmethod asfinal.As you found out,
finalwill prevent the method from being hidden by subclasses which is very good news imho.I’m quite surprised by your statement:
No, marking the method as
finalinFoowill preventBarfrom compiling. At least in Eclipse I’m getting:Also, I think people should always invoke
staticmethod qualifying them with the class name even within the class itself: