Static method access dilemma and proposal: @NotInherited

Zhong Yu zhong.j.yu at
Thu Jun 27 14:17:37 PDT 2013

Stephen, would it be more accurate to say that what you want is
actually non-"overridable". It's not a problem that B inherits foo()
from A. It is a problem that B may introduce its own foo(), therefore
the meaning of `` is a little uncertain.

Non-overridable can be achieved by "final" on instance methods.

For static methods, oddly enough, "final" has the same effect - it
prevents subclass to introduce a method with the same signature -

It is a compile-time error to attempt to override or hide a final method.

If a class declares a static method m, then the declaration m is said
to hide any method m', where the signature of m is a subsignature
(§8.4.2) of the signature of m', in the superclasses and
superinterfaces of the class that would otherwise be accessible to
code in the class.

Therefore if we have

    class A

        final static void foo(){}

    class B

        static void foo(){}

B should not compile. (Unfortunately, it does compile in javac 7. A bug?)

So can we say "final" static method already does what you want to achieve?

Zhong Yu

More information about the lambda-dev mailing list