//(result in confusing code)
class MyClass {
staticint x = 0;
MyClass() {
x++;
}
}
publicclass MainClass {
publicstaticvoid main(String[] argv) {
MyClass e1 = new MyClass();
MyClass e2 = new MyClass();
e1.x = 100;
e2.x = 200;
System.out.println(e1.x);
}
}
200
A better way to refer to a static variable is via the class name.
class MyClass {
staticint x = 0;
MyClass() {
x++;
}
}
publicclass MainClass {
publicstaticvoid main(String[] argv) {
MyClass e1 = new MyClass();
MyClass e2 = new MyClass();
MyClass.x = 100; // Why did I do this?
MyClass.x = 200;
System.out.println(e1.x);
}
}
200