Use Of Static Keyword In Java

Static Modifier in Java

In this article, we will discuss the static non-access modifier in Java. Non-access modifiers change the default behavior of the Java class and its members.

As explained below, the non-access modifier static in Java can be used with variables, methods, classes, and interfaces.

Static Variables in Java 

As we know, variables are associated with the class. Similarly, static variables belong to the class, and they are common to all the instances (reference variable of a class) and are shared by all the class objects in Java. If we don't want a variable to be object-specific, we can make it static.

Let us see this with an example,

package StaticModifier;    
class Employee {    
    int empid;    
    String empName;    
    String ceo;    
    void display() {    
        System.out.println(empid + " : " + empName + " : " + ceo);    
    }    
}    
public class StaticDemo {    
    public static void main(String[] args) {    
        Employee emp1 = new Employee();    
        emp1.empid = 1;    
        emp1.empName = "ABC";    
        emp1.ceo = "XYZ";    
        Employee emp2 = new Employee();    
        emp2.empid = 2;    
        emp2.empName = "DEF";    
        emp2.ceo = "XYZ";    
        emp1.display();    
        emp2.display();    
    }    
}

When this program executes, it will result in

1 : ABC: XYZ

2 : DEF: XYZ

As in one organization CEO is the same for all the employees, so we will change the visibility of the variable CEO to static as its values are the same for all objects irrespective of the object.

The above scenario can be implemented as,

package StaticModifier;    
class Employee {    
    int empid;    
    String empName;    
    static String ceo;    
    void display() {    
        System.out.println(empid + " : " + empName + " : " + ceo);    
    }    
}    
public class StaticDemo {    
    public static void main(String[] args) {    
        Employee emp1 = new Employee();    
        emp1.empid = 1;    
        emp1.empName = "ABC";    
        emp1.ceo = "XYZ";    
        Employee emp2 = new Employee();    
        emp2.empid = 2;    
        emp2.empName = "DEF";    
        emp2.ceo = "XYZ";    
        //this value will be replaced as ceo is a static variable    
        emp2.ceo = "PQR";    
        emp1.display();    
        emp2.display();    
    }    
}

When this program executes, it will result in,

1: ABC : PQR

2: DEF : PQR

Since the CEO is a static variable, the value will be the same for all objects irrespective of the object. Hence we get PQR as the CEO name attribute.

Points to remember

Since static variables belong to a class, we can use both object names and class names - both will work. But the preferred way to access them is by using the class name. If we make a variable static, it will not be any more for a particular object; it will be the same for all the objects. Normal variables are loaded with heap memory, but static variables are loaded with classloader memory. If a variable is static, the value will be the same for all objects, irrespective of the object.

Static Block in Java

A static block is a special block in Java that can be used to initialize a static variable. It can be easily explained when referenced with static or non-static variables since the above code describes the need for the static keyword in Java.

Let us demonstrate it with an example,

package StaticModifier;    
class Employee {    
    int empid;    
    String empName;    
    static String ceo;    
    //static block    
    static {    
        ceo = "XYZ";    
    }    
    public Employee() {    
        empid = 1;    
        empName = "ABC";    
    }    
    void display() {    
        System.out.println(empid + " : " + empName + " : " + ceo);    
    }    
}    
public class StaticBlockDemo {    
    public static void main(String[] args) {    
        Employee emp1 = new Employee();    
        Employee emp2 = new Employee();    
        emp1.display();    
        emp2.display();    
    }    
}

When this program executes, it will result in

1: ABC : XYZ

1: ABC : XYZ

Note

Static Block will be executed only once, irrespective of how many objects it will create.

Static Block is executed only once you load a class, i.e., only once.

We cannot use a non-static variable in a static block.

Static Methods in Java

These methods are not associated with objects. As explained above, we don't need any object to access a static variable. Thus a method can be marked as static; therefore, we don't need any object to call a static method.

static double average(int num1, int num2, int num3) {    
   return(num1+num2+num3)/3;    
}

As everyone who wrote code in Java had interacted with this static method,

public static void main(String[] args) {    
    //some code    
}

This main method is static, as referenced by the main class.

A simple example can demonstrate this,

package StaticModifier;    
class Employee {    
    int empid;    
    String empName;    
    static String ceo = "PQR";    
    //static block    
    Static void replace() {    
        ceo = "XYZ";    
    }    
    void display() {    
        System.out.println(empid + " : " + empName + " : " + ceo);    
    }    
}    
public class StaticBlockDemo {    
    public static void main(String[] args) {    
        Employee.replace(); //replace method is called    
        Employee emp1 = new Employee();    
        emp1.empid = 1;    
        emp1.empName = "ABC";    
        Employee emp2 = new Employee();    
        emp2.empid = 2;    
        emp2.empName = "DEF";    
        emp1.display();    
        emp2.display();    
    }    
}

When this program executes, it will result in

1: ABC : XYZ

2: DEF : XYZ

Points to remember

  • The static method can't use a non-static variable or non-static method directly.
  • We can't use this and super with static context.
  • We can use static methods and variables using a null reference.
  • We can't override the static members in a derived class.

Static Classes and Interfaces in Java

Static classes and interfaces are a type of nested class and interface in Java. Java uses the concept of nested class, i.e., a class defined under another class. The class in which the nested class is defined is the outer class.

As the inner class can access all the members of the outer class without using a reference to the outside class, thus it makes it more concise and simple.

Let us demonstrate it with an example,

package StaticModifier;    
public class StaticClass {    
    static class nestedClass {    
        public void show() {    
            System.out.println("This is a nested class.");    
        }    
    }    
    public static void main(String args[]) {    
        StaticClass.nestedClass nested = new StaticClass.nestedClass;    
        nested.show();    
    }    
}

When this program executes, it will result in the following:

This is a nested class.

Points to remember

We can't add static to the prefix of top-level class and interface.

For example,

static class Employee{}    
static interface EmpInterface{}

This code will fail to compile as adding static with a prefix of class or interface is not allowed.

An inner class can access the member of the outer class without its reference.

Summary

So at the end of this article, the static keyword is important when the same value is repeatedly used in the program. It will not impact objects, and if the variable is static, its value will be the same for all objects, irrespective of the object. The static method cannot use a non-static method or variable directly.


Similar Articles