Here we made a variable X as a static variable and if we try to access that from main() directly then it will not show any error and executed. It proves that all static things of a class always belong to a class. Thus, keep those properties of an object as a static whose values are same from each instance of an object.
Here is the source code of the Java Program to Demonstrate Usage of a Static Variable x in the Test Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Test
static int x = 17;
public static void main(String...a)
System.out.println(x); //static variable can be called directly
System.out.println(Test.x); //static variable can be called with class its name
$ javac Test.java $ java Test 17 17
Sanfoundry Global Education & Learning Series – 1000 Java Programs.