Using Static Variables in Java Last Updated : 22 Sep, 2021 Comments Improve Suggest changes Like Article Like Report 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 static variable, why to incorporate in our program. Now we are taking a real-time example as an illustration here in order to perceive why we are using it. Illustration: Employee directory Suppose there are 25 students in the Production Engineering department of NIT Agartala. All students have its unique enrollment number, registration number, and name. So instance data member is good in such a case. Now all instance data members will get memory each time when the object is created. Here, "department" refers to the common property of all the objects. If we make it static, this field will get the memory only once. Thus static variables can be used to refer to the common property of all objects (which is not unique for each object), for example, college name of students, company name of employees, CEO of a company, etc. It makes the program memory efficient (i.e., it saves memory). Example Java // Java Program to show the Advantages of Static Variable // Class 1 class emp { // Attributes of employees int id; String name; int salary; // Here we are declaring CEO as a static variable static String ceo; // Constructors of this class emp(int id, String name, int salary, String ceo) { // This keyword refers to current instance itself this.id = id; this.name = name; this.salary = salary; this.ceo = ceo; } // Method of this class void display() { // Print all associated attributes System.out.println("ID: " + id + ", " + "Name:: " + name + ", " + "Salary: $" + salary + " & " + "CEO:: " + ceo); } } // Class 2 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of class 1 // Object 1 emp Monodwip = new emp(1, "Monodwip", 30000, "Rinkel"); // Object 2 emp Mukta = new emp(2, "Mukta", 50000, "Rinkel"); // We have changed the CEO for Subham, As CEO is // declared static, sowill change for all the // objects // Object 3 emp Subham = new emp(3, "Subham", 40000, "Arnab"); // Calling display() method over all objects Monodwip.display(); Mukta.display(); Subham.display(); } } OutputID: 1, Name:: Monodwip, Salary: $30000 & CEO:: Arnab ID: 2, Name:: Mukta, Salary: $50000 & CEO:: Arnab ID: 3, Name:: Subham, Salary: $40000 & CEO:: Arnab Output explanation: From the above output, we can perceive that Rinkel got replaced by Arnab for all the objects, hence justifying the usage of static variable. Comment More infoAdvertise with us Next Article Using Static Variables in Java dattabikash505 Follow Improve Article Tags : Java Blogathon Blogathon-2021 java-basics Practice Tags : Java Similar Reads Static Variables in Java 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 u 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 Unnamed Patterns and Variables in Java Java programming language offers multiple features that allow developers to write rich and concise code. Among these features, there are unnamed patterns and variables which enables developers to work with data in a more flexible and elegant way. In this article, we will study about unnamed patterns 4 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 Static and Non Static Blank Final Variables in Java In Java, a variable provides us with named storage that our programs can manipulate. In Java, a blank final variable is a final variable that is declared but not immediately initialized. It must be assigned exactly once before use. When a variable is declared with static, it becomes a static blank f 6 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 8 min read Final Local Variables in Java In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u 3 min read final variables in Java In Java, we can use final keyword with variables, methods, and classes. When the final keyword is used with a variable of primitive data types such as int, float, etc), the value of the variable cannot be changed. Example 1: Usage of final with primitive datatype Java // Java Program to illustrate 2 min read Difference between static and non-static variables in Java There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is 4 min read Like