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

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.

Was this post helpful?

Related Posts

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusionWas this post helpful? An object in Java is an instance of a class. It is a […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in JavaWas this post helpful? In this post, we will see how […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusionWas this post helpful? In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classesWas this post helpful? In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusionWas this post helpful? In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusionWas this post helpful? If […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.