Static Variables in Java Last Updated : 15 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we use the static variable most of the time:Storing constant values or default values that can be used across different instances.The Static variable is used to store the data of class instances. To record the number of times a class instance is created.Important points:Class-Level: We can create static variables at the class level only. To know more, refer here.Shared Among Objects: The static blocks and static variables are executed in the order they are present in a program. It means if a static variable is modified by any instance, the changes will show in all other instances.Accessed through Class Name: Static variables can be called directly with the help of a class only; we do not need to create an object for the class in this.Initialization Order: The static variables are initialized before the static blocks, and when the static method or block refers to a static variable, then it will use its initialized value.Example: This example demonstrates how to use a static variable among different methods. Java // Java program to demonstrate execution // of static blocks and variables class Geeks { // static variable static int a = m1(); // static block static { System.out.println("Inside static block"); } // static method static int m1() { System.out.println("from m1"); return 20; } // static method(main) public static void main(String[] args) { System.out.println("Value of a : " + a); System.out.println("from main"); } } Outputfrom m1 Inside static block Value of a : 20 from main Explanation: In the above Java program, we use the static variable which store the value which is calculated by the m1() method.Difference Between Static Variable and Instance VariableAspectStatic VariableInstance VariableDefinitionThe static variable is is belong to the class and It shared the same instance in all the reference where it is used.The instance variable is belong to the instance of a class (object).MemoryThe static variable is allocated on the time of class loading in the memory only once.The instance variable is created when we create an object of a class.ModificationIf we update the value of the static variable it will reflect to all the instances where it is In use.The instance variable has the separate copies in each of it's instances.AccessWe can directly access it without creating an object of a class.The instance variable is only access via object of the class.ScopeThe scope of the static variable is in the entire class. We can access through all the instances of the class.It's scopeis limited to the specific instance of the defined class. Comment More infoAdvertise with us Next Article Static Variables in Java C code_r Follow Improve Article Tags : Java Static Keyword Practice Tags : Java Similar Reads Using Static Variables in Java Here we will discuss the static variables in java. Java actually doesnât have the concept of Global variable. To define a Global variable in java, the keyword static is used. The advantage of the static variable is discussed below. Now geeks you must be wondering out what are the advantages of stati 3 min read Final Static Variable in Java When the value of a variable is not varied, then it is not a good choice to go for an instance variable. At that time, we can add a static modifier to that variable. Whenever we declare a variable as static, then at the class level, a single variable is created which is shared with the objects. Any 3 min read Scope of Variables in Java The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about 7 min read Java Variables In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.Key Components of Variables in Java:A variable in Java has three components, which are listed below:Data Type: Defines the kind 9 min read Are Static Local Variables Allowed in Java? In Java, there are different types of variables, each with its own behavior and scope. Understanding these variables plays a very important role. In this article, we will discuss the concept of static with local variables, and we will also discuss in Java static local variables are allowed or not.Wh 3 min read Like