Inheritance in Static Methods

后端 未结 5 435
南笙
南笙 2020-12-01 18:51

Why does the below code print \"Main\"?

public class Main
{
    public static void method()
    {
        System.out.println(\"Main\");
    }

    public sta         


        
5条回答
  •  执笔经年
    2020-12-01 19:33

    Static methods are resolved on the compile-time type of the variable. m is of type Main, so the method in Main is called.

    If you change it to SubMain m ..., then the method on SubMain will be called.

提交回复
热议问题