calling a super method from a static method

后端 未结 6 1455
天涯浪人
天涯浪人 2020-12-05 18:14

Is it possible to call a super static method from child static method?

I mean, in a generic way, so far now I have the following:

public class BaseCo         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 18:59

    There is static inheritance in Java. Adapting the example from Nikita:

    class A {
        static void test() {
            System.out.print("A");
        }
    }
    class B extends A {
    }
    
    class C extends B {
        static void test() {
            System.out.print("C");
            B.test();
        }
    
        public static void main(String[] ignored) {
           C.test();
        }
    }
    

    This now compiles, and invoking C prints "CA", of course. Now we change class B to this:

    class B extends A {
        static void test() {
            System.out.print("B");
        }
    }
    

    and recompile only B (not C). Now invoking C again, it would print "CB".

    There is no super like keyword for static methods, though - a (bad) justification may be that "The name of the super class is written in the declaration of this class, so you had to recompile your class nevertheless for changing it, so you could change the static calls here, too."

提交回复
热议问题