Java - Static blocks

Java

Share

In this tutorial we will learn about static block of a class in Java programming language.

In the previous tutorials we learned about static variables and static methods. Now we will talk about the static blocks.

Use of static block

We use the static block if we want to initialise static variables. The static block is executed only once when the class is loaded.

Syntax of static block

static {
  // some code...
}

Where, static keyword is used to define the static block. The opening curly bracket { marks the starting of the static block and the closing curly bracket } marks the ending of the static block.

Example

In the following example we have the HelloWorld class and it has a static integer variable objectCount.

We will first initialise this static integer variable inside a static block.

Note! The value of this static integer variable is incremented whenever a new object of the class is created.

We also have the getCount() and resetCount() static methods to get and reset the value of the objectCount variable respectively.

class HelloWorld {

  // static variable
  static int objectCount;

  // variable
  private int number;

  // static block
  static {
    System.out.println("Inside the static block of HelloWorld class.");
    System.out.println("Initialising objectCount static variable.");
    objectCount = 0;
    System.out.println("Done initialising objectCount static variable of HelloWorld class inside the static block.");
  }

  // constructor
  HelloWorld(int number) {
    this.number = number;

    // updating the static variable
    HelloWorld.objectCount++;
  }

  // static method
  static void resetCount() {
    objectCount = 0;
  }

  static int getCount() {
    return objectCount;
  }

  // method
  public int getNumber() {
    return this.number;
  }
}

public class Example {
  public static void main(String[] args) {

    System.out.println("Total number of objects of the Hello World class: " + HelloWorld.objectCount);

    // create an object
    System.out.println("Creating object obj.");
    HelloWorld obj = new HelloWorld(10);

    System.out.println("Total number of objects of the Hello World class: " + HelloWorld.objectCount);

    System.out.println("Hello World obj number: " + obj.getNumber());

    // create another object
    System.out.println("Creating object obj2.");
    HelloWorld obj2 = new HelloWorld(20);

    System.out.println("Total number of objects of the Hello World class: " + HelloWorld.objectCount);

    System.out.println("Hello World obj2 number: " + obj2.getNumber());

    System.out.println("Resetting object count of the Hello World class.");
    HelloWorld.resetCount();
    System.out.println("Total number of objects of the Hello World class after reset: " + HelloWorld.getCount());

  }
}

Output:

$ javac Example.java 
$ java Example
Inside the static block of HelloWorld class.
Initialising objectCount static variable.
Done initialising objectCount static variable of HelloWorld class inside the static block.
Total number of objects of the Hello World class: 0
Creating object obj.
Total number of objects of the Hello World class: 1
Hello World obj number: 10
Creating object obj2.
Total number of objects of the Hello World class: 2
Hello World obj2 number: 20
Resetting object count of the Hello World class.
Total number of objects of the Hello World class after reset: 0

When the HelloWorld class first loads, the static block is executed and it sets the value of the static integer variable objectCount to 0.

Inside the Example class we are instantiating two objects of the HelloWorld class.

So, each time an object of the HelloWorld class is created, inside the constructor of the HelloWorld class the value of the static variable objectCount is incremented by 1 using the ++ increment operator.

Share

Recently Updated