类中静态方法的调用

package NowCoder;
class Test {
    public static void hello() {
        System.out.println("hello");
    }
}
public class MyApplication {
    public static void main(String[] args) {
      
        Test test=null;
        test.hello();
    }
}
结果
hello

 

解析:

静态方法是属于类的,与对象无关,只要是使用到了Test类,都会加载静态hello方法!

空指针必须是去引用堆对象才会有空指针,而这个hello是static类型的,人家static的方法本身就没有指针,所以当然不会有空指针。

引用类型  引用名=null,其实就是指该引用在堆中没有对应的对象,但是编译的时候还是能根据声明找到其所属的静态类型。


 
 
posted @ 2019-06-03 20:07  千里之外kb  阅读(5502)  评论(0编辑  收藏  举报