对于访问PrintStram类的方法,必须创建一个对象,以便out变量在分配null时如何访问这些方法。如何在System类中定义为static并指定为null的Out变量可以访问PrintStream类的非静态方法。
public final static PrintStream out = null;
这是System类中的声明。
我试图编写类似的代码,但它然后提供NullPointerException。我的代码如下。
class First{
public void display(){
System.out.println("Hello");
}
}
class Second{
public final static First s1=null;
}
public class Third{
public static void main(String[] args) {
Second.s1.display();
}
}
为了使该代码运行我将不得不使任一显示方法静态或定义S1原样
public final static First s1=new First();
它在运行时不'null'。 – Kayaman
本机代码为System.out分配了一个非空值。你的班级没有本地代码。 – VGR