Static keyword in Java4 min read

What is Static Keyword in Java?

Keywords are reserved terms in Java that can’t be used as identifiers. In Java, there are a total of 57 keywords. “Static” is one of them. In this post, we will cover static keywords in Java which is used in a variety of programming situations.

  • Sometimes we may require to define a class member that will be used independently of any object of that class.
  • Such members can be declared using keywords static. Both a class member variable and method can be static All instances of the class share the same static variable.
  • When we declare any member as static, it can be accessed before any object of its class is created and without reference to any object.
  • In every main (), the keyword static allows it to be called by a Java interpreter before any objects are made.
  • An instance variable declared as static must be a global variable. When objects of its class are declared, no copy of a static variable is made.
  • All instances of the class share the same static variable.
  • A static variable is allocated when the class is loaded. It belongs to the class and not to any object of the class.
  • A static method belongs to the class and not to any object of the class.

The static keyword can be:

  1. Static Block
  2. Static Variable
  3. Static Method
  4. Static Classes

Static Block

You can declare a static block that gets executed exactly once when the class is first loaded if you need to do the computation to initialize your static variables. Take a look at the Java program below to see how to use Static Block.

// Java program to demonstrate the use of static blocks
import java.util.*;
public class StaticBlockExample{
  // static variables
  static int num1 = 10, num2 = 5;
  static int add;.

  // static block
  static{
    System.out.println("in Static block);
    add = num1 + num2;
  }

  public static void main(String[] args){
    System.out.println("in main method");
    System.out.println("Value of add : " +add);
  }
}

When you run the above program, the static block is initialized, and the values of the initialized variables are displayed.

Output:

in Static block
in main method
Value of add : 15

Static Variable

Static variables are the variables that are initialized at the time of declaration. That is, when you create a static variable, you have to assign a value to it. A static variable belongs to the class and not the class object. For this reason, it is not required to create an object of the class to call a static variable. It can be called directly from the static methods. Also, if you want to call a static variable from a different class, you just need to use the ClassName.staticVariable to call the static variable. To further understand how to use the Static variable, look at the Java program below.

//Java Program to demonstrate the use of static variable  
class Student{  
   int rollNo; //instance variable  
   String name;  
   static String college ="ITS"; //static variable  
  
   //constructor  
   Student(int r, String n){  
     rollNo = r;  
     name = n;  
   }  
  
  //method to display the values  
  void display(){
    System.out.println(rollNo + " " + name + " " + college);
  }  
} 

//Test class to show the values of objects  
public class TestStaticVariable1{  
  public static void main(String[] args){  
    System.out.println("college : " + college); //here we can call the variable since it is static or else an error would be thrown
    Student s1 = new Student(11,"Karan");  
    Student s2 = new Student(2,"Aryan");  
    //we can change the college of all objects by the single line of code  
    //Student.college="BBDIT";  
    s1.display();  
    s2.display();  
  }  
}  

Output:

college : ITS
11 Karan ITS
2 Aryan ITS

Static Method

  • Any method that uses the static keyword is referred to as a static method.
    • A class’s static method, rather than the class’s object, belongs to the class.
    • A static method can be called without having to create a class instance.
    • A static method can access and update the value of a static data member.

Example:

//Java Program to demonstrate the use of a static method.  
class Student{ 
  int rollno;  
  String name;  
  static String college = "ITS";  
  //static method to change the value of static variable  
  static void change(){  
    college = "BBDIT";  
  }  
  //constructor to initialize the variable  
  Student(int r, String n){  
    rollno = r;  
    name = n;  
  }  
  //method to display values  
  void display(){
    System.out.println(rollno+" "+name+" "+college);
  }  
}  

//Test class to create and display the values of object  
public class TestStaticMethod{  
  public static void main(String[] args){  
    Student.change(); //calling change method  
    
    //creating objects  
    Student s1 = new Student(111,"Karan");  
    Student s2 = new Student(222,"Aryan");  
    Student s3 = new Student(333,"Sonoo");  
    
    //calling display method  
    s1.display();  
    s2.display();  
    s3.display();  
  }  
}  

Output:

111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT

Static Classes

If a class is nested, it can only be made static. A reference to the Outer class is not required for a nested static class. In this scenario, a static class can’t access the Outer class’s non-static members.

public class NestedExample{
  private static String str = "Hiberstack";
    //Static class
    static class MyNestedClass{
      //non-static method
      public void disp(){
        System.out.println(str);
      }
    }
  
  public static void main(String[] args){
    NestedExample.MyNestedClass obj = new NestedExample.MyNestedClass();
    obj.disp();
  }
}

Output:

Hiberstack

Share:

Leave a Reply