Static block in java

In this post, we will see about how to implement static block in java.

Here are some important points about static block in java.

  1. Static block is used for static initializations of a class
  2. Static block is executed only once when either you create an object of the class or access static member of the class
  3. Static block is executed before constructor of object is called.

Syntax

Example

Let’s see with the help of example.
Create a class App.java

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Create a Main class named StaticBlockMain.java

When you run above program, you will get below output:

Static block is called
100

As you can see, when we accessed static variable of class, static block was called.
Let’s see another example and change content of StaticBlockMain.java as below.

Output:

Static block is called
Constructor is called
Static variable: 100
Instance variable: 300

As you can see, static block is called before constructor of object is called.
You can not use instance variable in static block.
This is similar to static method, you can not use instance variable in static block otherwise you will get compilation error with Cannot make a static reference to the non-static field b.

Use multiple static blocks

You can use multiple static blocks in class and they will called from top to down.

Let’s understand with the help of example.

Create main class StaticBlockMain.java

Output:

Static block 1 is called
Static block 2 is called
Static block 3 is called
Static variable 1: 100
Static variable 1: 200
Static variable 1: 300

When we first accessed static variable App.a,all static blocks were called in top to down sequence.
That’s all about static block in java.

Related Posts

  • How to end program in java
    08 March

    How to end program in java

    Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program You can use exit() method of System class to end java program. System.exit() causes Java virtual machine to exit and terminate the current process. You can call […]

  • 25 September

    Reference Variable in Java

    In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 17 June

    How to learn Java Programming

    Java is one of the most used programming languages in the world for developing the desktop application and web application.It has completed 22 years in the programming world. In this post, we will see how to learn java programming in depth and in most efficient way. Java has a huge learning curve, it is a […]

  • 28 May

    Introduction to Java Programming

    Java is one of the most used programming languages in the world for developing desktop application and web application. It has completed 22 years in the programming world. History of Java: Java programming language was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in 1991. The initial name was Oak but […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.