Exploring Static Blocks in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Exploring Static Blocks in Java

Share This
Well, you might have worked with static variables and static methods, but in general static blocks are rarely explored. A static block is a block of code that is defined within the class to be accessed before every other block of codes can be accessed. In this tutorial, we are going to explore the static block and some of its properties
  

Can we have multiple Static Blocks within a Class?

Yes! We can have that.

You can have more than one static block within a class and the static blocks will function as per their position within the class. I mean the static block which has been defined earlier, would be accessed earlier. The following code will prove this point: 

Code of Sample.java
public class Sample { static { System.out.println("I am in first static block "); } static { System.out.println("I am in second static block"); } static { System.out.println("I am in third static block"); } }

Code of TestSample.java
public class TestSample { public static void main(String[] args) { Sample s = new Sample(); } }

In the above example, Sample is the main class which has three static blocks and TestSample is the driver class which instantiates the Sample class. As shown in the following figure, the output is as expected. The static block which has been defined first has been accessed first, and the static block which has been defined last has been accessed last.


The output of the example code



Can we place a static block anywhere?


Yes! You can place static blocks anywhere in the class. The position does not matter, what matters is the order of the positioning. You can even write a static block after the main function, still, it will work. The following code will illustrate this idea.

Code of Test.java
public class Test { static{ System.out.println("I am in first static block and defined above main."); } static { System.out.println("I am in second static block and defined above main."); } public static void main(String[] args) {// TODO Auto-generated method stub } static { System.out.println("I am in third static block and defined below main."); } }

This code when runs produce the following output where three static blocks work as per their order of definition irrespective of their positions, whether defined before or after the main function

The output of the above code



Will Constructor be accessed before a static block?


Never. All the static blocks will be accessed before the constructor is accessed. You can check out the following codes and their corresponding output which supports this notion.

code of Demo.java
public class Demo { static { System.out.println("I am in static block"); } public Demo() { System.out.println("I am in constructor"); } }

code of DemoDriver.java
public class DemoDriver { public static void main(String[] args) { Demo d = new Demo(); } }

The output of the DemoDriver

Lastly, it is noteworthy that within a static block, both static and non-static variables can be accessed.
We hope you have thoroughly enjoyed this tutorial!

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.