How to create static variables in Java?
Class variables or static variable is variable that declared with static modifier. A given class will have only one copy of each of its static variables, regardless of how many times the class has been instantiated.
If the value of a static variable is changed, the new value is available equally in all instances of the class. The final keyword could be added to indicate the value of static variable will never change.
If you try to assign a new value to final variable, you will get a compile error.
Here is the output printed by the program:
x before update = 12
x after update = 15
StaticDemo.x = 20
sd0 = 20
sd1 = 20
sd2 = 20