I need an explanation for the output of the below code:
class Stats
{
static int a = 10;
int b = 20;
void printMe()
{
System.out.println(a+b);
}
}
public class Static
{
public static void main(String args[])
{
Stats s1 = new Stats();
Stats s2 = new Stats();
s1.b = 30;
s1.printMe();
s1.a = 20;
s2.printMe();
}
}
Output: 40 40
I expected it to be 40 and 50 as there should be only one copy of static variable 'a' which is modified by through reference 's1' to 20.