Why isn't calling a static method by way of an instance an error for the Java compiler?

后端 未结 12 2020
北海茫月
北海茫月 2020-11-22 06:59

I\'m sure you all know the behaviour I mean - code such as:

Thread thread = new Thread();
int activeCount = thread.activeCount();

provokes

12条回答
  •  有刺的猬
    2020-11-22 07:45

    Likely for the same logical that makes this not an error:

    public class X
    {
        public static void foo()
        {
        }
    
        public void bar()
        {
            foo(); // no need to do X.foo();
        }
    }
    

提交回复
热议问题