Static block in Java is used for static initializations of a class. Consider a scenario where 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.
In case of instance variables, initialization can be done in constructors, where exception handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.
Refer Initializer block in Java to see another way to initialize instance variables.
static Block is Executed Only Once
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 create an object of that class or when you access the static member of that class for the first time.
General form of the Java static block
static{ // whatever code is needed for initialization goes here }
static Block Java Example
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 in Java 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 in Java 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
Java static block exception handling
static block can throw only RunTimeException, or there should be a try-catch block to catch checked exception.
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!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment