Introduction
Like C++, Java supports a special block, called a static block (also called static clause) which can be used for static initializations of a class. This code inside static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class). For example, check the output of the following Java program.
static int i; int j; // start of static block static { i = 10; System.out.println("static block called "); } // end of static block } class Main { public static void main(String args[]) { // Although we don't have an object of Test, static block is // called because i is being accessed in following statement. System.out.println(Test.i); } } static block called 10
class Test {
Also, static blocks are executed before constructors. For example, check the output of the following Java program.
static int i; int j; static { i = 10; System.out.println("static block called "); } Test(){ System.out.println("Constructor called"); } } class Main { public static void main(String args[]) { // we have two objects but, Static block is executed only once. Test t1 = new Test(); Test t2 = new Test(); } } Output: static block called Constructor called Constructor called
class Test {
A Java programmer initializes variables in a constructor (or init() method in case of the applet). It is the best place chosen, as the constructor is called implicitly when an object is created. A programmer creates objects before anything is done in coding (as the object is required to call an instance variable or method). Now read on Static Blocks.
In the place of a constructor, a static block can be chosen, if the programmer does not like to have a constructor. One more advantage is static block is executed even before the main() method is executed. That is, Java execution starts from static blocks and not from the main() method.
Three programs are given on static blocks; each differs slightly.
In the following program, the instance variables are static. As they are static, they called from main() without the help of an object.
{ static double $rate; static int numOfDollars; static { $rate = 75.80; numOfDollars = 12; System.out.println("I am static block, I am called first."); } public static void main(String args[]) { Demo d1 = new Demo(); System.out.println("I am main() method, executed after static block."); System.out.println("Dollar value in Rupees: " + $rate * numOfDollars); } } output: I am static block, I am called first. I am main() method, executed after static block. Dollar value in Rupees: 909.56
public class Demo
Multi Static Blocks
A program can have any number of static blocks.
{ static { System.out.println(); System.out.println("First static block."); } static { System.out.println("Second static block."); } static { System.out.println("Third static block."); } } output: First static block. Second static block. Third static block. Exception in thread “main” java.lang.NoSuchMethodError: main
public class MultiStaticBlocks
Observe, purposefully, the main() method is not included in the program. Even then, the program compiles and runs as static blocks are called before main(). Look at the screenshot, the main() is checked by the JVM after calling all static blocks. As the main() is not available, an exception is thrown...
JDK 1.7 Modifications
From JDK 1.7, the main() method is required to execute a static block. Anyhow, a static block is called before the main() is executed.
0 Comments
Post a Comment