Static in Java

The Static keyword in Java is used mainly for memory management. The static keyword belongs to the class than an instance of the class, which means if you make a member as static then you can access it without creating a class object.

The static keyword can be used as :

  1. A variable
  2. A method
  3. A class
  4. A Block of statements

1. Java Static variable

It is common to all instances(or objects) of the class because it is a class level variable. It also means that the static variable is created only once and is shared among all the other instances of the class. A static variable can be accessed directly by the class name and doesn’t need any object.

<class-name>.<variable-name>

When and why we use static variable?

Scenario –

  1. Things like Database connection url , which may not change for a particular database
  2. An employee class, it has many employees with unique employee ids but belongs to the same company, hence company name can be put into a static variable.

Example:

public class StaticVariableTestClass {

  static int a1;
  static String a2;
  
  // This is a Static Method, calling static variables
  static void display() {
      System.out.println("Variable one is: " + a1);
      System.out.println("Variable two is: " + a2);
  }

  public static void main(String args[]) {
      display();
  }

}
  

OUTPUT

Variable one is: 0
Variable two is: null

Another Usecase : counter ++ using static variable, used as a counter variable.

2. Java Static method

  1. Static method in Java is a method which belongs to the class and not to the object. A static method can access only static data(not the instance variables)
  2. A static method can call only other static methods from it and cannot call a non-static method.
  3. A static method cannot refer to “this” or “super” keywords.
  4. A static method can be accessed directly by the class name , object instance is not needed.
<class-name>.<method-name>
public class TestStaticVariable {

  static int sum;

  // This is a Static Method
  static int sumOfTwoNumbers(int a1, int a2) {
    sum=a1+a2;
    return sum;
  }

  public static void main(String args[]) {
    sum=sumOfTwoNumbers(5,5);
    System.out.println("The sum of two numbers is : "+sum);
  }

}  

OUTPUT

The sum of two numbers is : 10

3. Java Static block

  1. Static block is a block where you can write statements to declare static variables.
  2. Static block is the first thing which JVM initializes on class load.
public class StaticBlock {

  static int number;

  static int square;

  // This is a Static Block
  // No matter where it is placed(top or bottom of code) it gets called 
  // before any other method
  static {
    System.out.println("** inside static block to initialize static variable **");
    number = 5;
  }

  // This is a Static Method
  static int squareOfANumber(int a1) {
    square = a1 * a1;
    return square;
  }

  public static void main(String args[]) {
    square = squareOfANumber(number);
    System.out.println("The square of a " + number + " is : " + square);
  }

}
    

OUTPUT

** inside static block to initialize static variable **
The square of a 5 is : 25

4. Java Static class

  1. A class can be made static only if it is a nested class which also means We can not declare top-level class with a static modifier.
  2. Nested static class doesn’t need reference of Outer class.
  3. A static class cannot access non-static members of the Outer class.
  4. A non static nested class is called the inner class.
             
public class OuterClass {

 private static String str = "ProgrammerToday";

   // nested class - as it is (inner + static)
   static class NestedStaticClass {

   // static method inside nested class
   static void displayName() {
     System.out.println("Book Name is : " + str);
   }

   // non-static method inside nested class
   void displayContent() {
     System.out.println("This book is about technology");
   }
   }

 public static void main(String[] args) {

 // way to call static method of nested class
 OuterClass.NestedStaticClass.displayName();

 // way to call non-static method of nested class by creating instance
 OuterClass.NestedStaticClass obj = new NestedStaticClass();
 obj.displayContent();

 }

}

OUTPUT

Book Name is : ProgrammerToday
This book is about technology

Summary

In this article we learnt about Java Static Keyword and its various use cases. We sar it can be used as a variable, method, block and a class.
Hope you liked the article !