You should confuse the use of static variables .
static class variables are created once for each class. They are shared by all instances of the class, where non-static class variables are created for each instance of the object.
So, if your variable is counter static , it will be created only once and will be used by all instances of your class.
When you access it using MyObject.counter or object1.counter , etc., you get access to the same counter variable that static variables with the class name can access, as well as with the instance variable.
And if it is non-static , and each instance (or object) of your class will have its own copy of counter .
So each of your Object1 , Object2 , etc. Will have its own variable counter .
And they will all have a value of 1, and so you get 1 on the output.
UPDATE:
Modify your code to get the desired result that you mentioned in a comment on one of the answers:
MyObject Object1 = new MyObject(); System.out.println("Value of Counter for Object 1: " + Object1.counter); MyObject Object2 = new MyObject(); System.out.println("Value of Counter for Object 2: " + Object2.counter); MyObject Object3 = new MyObject(); System.out.println("Value of Counter for Object 3: " + Object3.counter); MyObject Object4 = new MyObject(); System.out.println("Value of Counter for Object 4: " + Object4.counter); MyObject Object5 = new MyObject(); System.out.println("Value of Counter for Object 5: " + Object5.counter); System.out.println("Value of instanceCounter for Object 1: " + Object1.instanceCounter); System.out.println("Value of instanceCounter for MyObject: " + MyObject.instanceCounter);
source share