Object调用静态方法

谁说空指针不能调用方法

public class Foo {

    public static void bar() {
        System.out.println("bar");
    }

    public static void main(String[] args) {
        Foo f = null;
        f.bar();      // 执行不会报空指针异常
    }
}

以上f.bar() 等价于 Foo.bar(),跟实例本身无关。

原文地址:https://www.cnblogs.com/xsj24/p/5956647.html