Here's simple class that has two instance variables, one static int and one just int. Why am I getting this output?
Code:
public class IdentifyMyParts { public static int x = 7; public int y = 3; public static void main(String[] args) { IdentifyMyParts a = new IdentifyMyParts(); IdentifyMyParts b = new IdentifyMyParts(); System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x); System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " + b.x); //now I will try to change the values: a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " + b.x); System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x); } }
My ouput:
> java IdentifyMyParts IdentifyMyParts.x = 7 a.y = 3 b.y = 3 a.x = 7 b.x = 7 a.y = 5 b.y = 6 a.x = 2 b.x = 2 IdentifyMyParts.x = 2
As you can see, I tried to change a.x to 1, but get 2.
b.x changed.
I thought static variables were inaccessible.
Clarify please. Thanks