Static Variables in Java Last Updated : 01 Aug, 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.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 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: We use the static variable which store the value which is calculated by the m1() method.When the class is loaded, static members are initialized first. m1() method is called and print the message.Then all static variables are initialized and the static block is executed.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.Related articles: Are Static Local Variables Allowed in Java? Comment More info C code_r Follow Improve Article Tags : Java Static Keyword Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like