不,只有一个变量-您尚未声明任何实例变量。
不幸的是,Java允许您访问静态成员,就像通过相关类型的引用访问静态成员一样。这是IMO的设计缺陷,某些IDE(例如Eclipse)允许您将其标记为警告或错误-
但这是语言的一部分。您的代码有效:
System.out.println("Value of i = " + test.i);System.out.println("Value of static i = " + test.i);如果您 确实 使用了相关类型的表达式,则它甚至不会检查该值-例如:
test ignored = null;System.out.println(ignored.i); // Still works! No exception
不过,仍然会评估任何副作用。例如:
// This will still call the constructor, even though the result is ignored.System.out.println(new test().i);



