Pages

Static variable initialization in Java

There are two types of variable in Java classes

1. Class variable (static variable)
2. Instance variable

Class variable (static variable)

The variables defined with "static" keyword is known as class variable, because these are associated with class and created on class memory.

Syntax for static variable

 public static String staticVariable;  

Properties of static variable


1. These variables are share with all object of the class, and there is a single copy of static variable. if one object change the value of static variable then changes visible to all variable.

2. Static variables are initialized only once , at the start of the execution .

3. A static variable can be accessed directly by the class name and doesn’t need any object.

4. These variables will be initialized first, before the initialization of any instance variables

Instance variable

The variable define without static keyword is known as "instance" variable or object variable.


Properties of instance variable



1. These variable associated with object of class.
2.   each object have its own local copy for variable.
3. require instance to access this.


Initialization of static variable 

1. you can initialize it at the time of declaration
2. you can do by making static block
3. alternative to static blocks — you can write a private static method

Initialize the variable at the time of declaration

Example 

public class StaticBlock {

 private static String staticVariable="I am static variable";
 private String instanceVariable;
 
 public static void main(String[] args) {

  System.out.println(staticVariable);

 }

}

Output

I am static variable
when the class loaded static variable initialized

Static Initialization Block

static variable can initialized using static block.
when class loader loads the class static block executed automatically.

syntax for static block

static {
    // whatever code is needed for initialization goes here
}


Here is an example of static initialization block

public class StaticBlock {

 private static String staticVariable;
 private String instanceVariable;

 static {
  staticVariable = "I am static variable";

 }

 public static void main(String[] args) {

  System.out.println(staticVariable);

 }

}

Output

I am static variable

Here we can see that as jvm loads the main class, the static block executed and initialize the variable. No object is created of the class.


Note :

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.


Example of multiple static block

public class StaticBlock {

 private static String staticVariable1;
 private static String staticVariable2;

 static {
  staticVariable1 = "I am static block 1";
  System.out.println(staticVariable1);

 }

 static {
  staticVariable2 = "I am static block 2";
  System.out.println(staticVariable2);

 }
 
 public static void main(String[] args) {
  
 }
 
 static {
  staticVariable2 = "I am static block 3";
  System.out.println(staticVariable2);

 }

}

Output

I am static block 1
I am static block 2
I am static block 3


How to write an immutable Class in Java

No comments:

Post a Comment