//(result in confusing code)
class MyClass {
static int x = 0;
MyClass() {
x++;
}
}
public class MainClass {
public static void 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 {
static int x = 0;
MyClass() {
x++;
}
}
public class MainClass {
public static void 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
|