Inheritance in Static Methods

后端 未结 5 444
南笙
南笙 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:53

    Java performs early binding for static methods, unlike instance methods which are dynamically bound.

    Because your object variable is of type Main the call is bound to the superclass implementation at compile time.

    A good explanation is available here.

提交回复
热议问题