Example:
class Demo { private static int increment = 1; public int number; public Demo() { number = increment; increment++; } } class App { public static void main(String[] args) { Demo demo1 = new Demo(); System.out.println("Demo1 value: " + demo1.number); Demo demo2 = new Demo(); System.out.println("Demo2 value: " + demo2.number); } }
Output:
Demo1 value: 1 Demo2 value: 2
I thought that there was always only one copy of a static variable, but how come in the output the values for each object is different? Is there any way to set both of their numbers to the same static value?