let a A.java file be :
class B {static int i; }
class A {
public static void main(String[] args) {
B a=new B();
B b=new B();
a.i=10;
b.i=5;
System.out.println(a.i);
}
}
Why is the result 5 and not 10 ?
Thanks.
Because your variable is static. That means it’s related to the type, not to any particular instance of the type. Your code is equivalent to:
IMO it was a design mistake to allow access to static members via expressions like this – and in some IDEs (e.g. Eclipse) it can end up giving a warning or even an error if you so wish.