Friday, 26 February 2016

static block in Java

Static block is used for static initializations of a class. Consider the scenario when initialization of static variables requires some logic (for example, error handling or a for loop to fill a complex array). In such scenarios, simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.

A static block in a class is executed only once, when the class is first loaded, even before the main method. Note that class can be loaded when you make an object of that class or when you access the static member of that class for the first time.

General form of the static block

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

Example code

public class StaticBlockDemo {
  // static blank final variable
  static final int i;
  static int b;
  //static block
  static {
   System.out.println("in static block");
   i = 5;
   b = i * 5;
   System.out.println("Values " + i + " " +  b);
  }
  
  public static void main(String[] args) {
  System.out.println(" in main method ");

  }
}

Output of the program

in static block
Values 5 25
in main method

It can be seen that main method is called only after executing the static block. In static block the static blank final variable is initialized. Also another static variable too is initialized with in the block after some computation.

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.

Since static blocks are used to initialize the class variables so static blocks are called before the constructor of the class.

public class StaticBlockDemo {
  // static blank final variable
  static final int i;
  static int b;
  //static block
  static {
   System.out.println("in static block");
   i = 5;
   b = i * 5;
   System.out.println("Values " + i + " " +  b);
  }
  
  StaticBlockDemo(){
   System.out.println("In constructor");
  }
  
  public static void main(String[] args) {
  System.out.println("in main method ");
  StaticBlockDemo sb = new StaticBlockDemo();

  }
  
  static {
   System.out.println("In another static block");
  }

}

Output

in static block
Values 5 25
In another static block
in main method 
In constructor

throw from a static block

static block can throw only RunTimeException, or there should be a try-catch block to catch checked exception.

Example Code

static int i;
static int b;
static {
 System.out.println("in static block");
 try{
  i = 5;
  b = i * 5;
 }catch(Exception exp){
  System.out.println("Exception while initializing" + exp.getMessage());
  throw new RuntimeException(exp.getMessage()); 
}
 //System.out.println("Values " + i + " " +  b);
}

That's all for this topic static block in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. static in Java
  2. static method overloading or overriding in Java
  3. static import in Java
  4. Why main method static in Java
  5. interface static methods in Java 8
  6. Initializer block in Java

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment