Using Static Variables in Java - GeeksforGeeks

22 Sep, 2021
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 1class 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 classclass 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();    }}
ID: 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.
Blogathon-2021
java-basics
Blogathon
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install Numpy on MacOS?
Dart - Null Aware Operators
How to Create a Table With Multiple Foreign Keys in SQL?
How to Install Tkinter in Windows?
Format Dates in Flutter
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
For-each loop in Java
Initialize an ArrayList in Java