Java

How to Perform Static and Instance Initialization in Java

In Java programming, there can be a requirement for the programmer to prioritize certain functionalities such that they come into effect before the actual implementation. For instance, logging a disclaimer or warning before heading to the actual code. In such cases, “static” and “instance” initialization in Java assists the developer in sorting the implemented features.

This article will demonstrate how to perform the “static” and “instance” initialization in Java.

How to Perform “Static Initialization” in Java?

The “static” initialization is done while the class is loading and works only with static variables. Moreover, it cannot pass reference via “this” and runs only once during the entire execution of the code when the class loads into the memory.

Note: Both the static and initialization blocks execute before the class constructor.

Example 1: Static Initialization in Java

In this example, the static initialization can be performed:

class Init {
static int x;
static {
 System.out.println("This is Static Initialization block!");
 x = 5;
}
Init() {
 System.out.println("This is Class Constructor!");
}}

public class staticinit {
public static void main( String args[] ) {
 Init obj = new Init();
 System.out.println(Init.x);
}}

In the above lines of code, apply the following steps:

  • First of all, define a class named “Init”.
  • In its definition, specify the static variable and define it in the “static” block along with the stated message.
  • In the next step, include the class constructor and log the provided message, as its definition.
  • In the “main” method, create an object of the class via the “new” keyword and the “Init()” constructor, respectively, and refer to the initialized integer within the class.
  • As a result, the static block executes prior to the class constructor, and the integer displays at last in accordance with the invoked sequence (in main).

Output

In this output, it can be analyzed that the static initialization is applied upon the static variable and the static block executes before the constructor.

How to Perform “Instance Initialization” in Java?

The “instance” initialization, on the other hand, utilizes both the static or non-static (instance) variables and can utilize “this” to pass reference. Also, it can execute multiple times in accordance with the call to the constructor.

Example 2: Instance Initialization in Java

In this particular example, the instance initialization can be applied:

class Init2 {
int x;
{
System.out.println("This is Instance Initialization block!");
x = 5;
}
Init2(int x) {
 System.out.println("This is Class Constructor!");
 this.x = x;
}}public class initinst {
public static void main( String args[] ) {
Init2 object1 = new Init2(8);
System.out.println(object1.x);
Init2 object2 = new Init2(10);;
System.out.println(object2.x);
}}

In this code block, perform the below-provided steps:

  • Likewise, declare the class named “Init2” and specify a non-static variable defined after the message.
  • In the next step, include a parameterized constructor accumulating the integer identical to the initialized one, as its parameter.
  • In its definition, print the provided message and refer to the initialized integer via “this” and allocate it to the passed integer argument.
  • In the “main”, create an object of the class and pass the specified integers twice as the constructor’s arguments since the instance initialization can be done multiple times.

Output

In this outcome, it can be implied that the instance initialization is done appropriately.

Conclusion

The “static” initialization works only with static variables and does not use “this” whereas the “instance” initialization utilizes both the static or non-static variables and uses “this”. Both of these initialization blocks are invoked before the class constructor. This write-up discussed the approaches to applying static and instance initialization in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.