Accessing variables in other classes (Java)
问题 Why doesn't the following program return 0 , since I am accessing p from a new A() , which has not had main called on it? public class A { public static int p = 0; public static void main(String[] args) { p = Integer.parseInt(args[0]); new B().go(); } } class B { public void go() { System.out.println(new A().p); } } 回答1: That should not even compile. Probably you had p as static and than you change it. The way it is written now, doesn't compile. $ javac A.java A.java:7: non-static variable p