JAVA BLOCKS AND SOME EXAMPLES OF INTERVIEW QUESTIONS

Serdar A.
5 min readJul 17, 2022

--

Click here for the Turkish of this article

In Java, apart from methods and constructors, Java has blocks called Initialization Blocks.

This blocks are :

  • Static Initialization Blocks
  • Instance Initialization Blocks

Static Initialization Blocks

Declared by using the keyword “static”. Runs first when the class is first loaded.

java-static-block

Instance Initialization Blocks

The code is copied into the beginning of each constructor of the class. So all your constructor classes share same initializer block. This is common place of your all constructor classes and also subclasses can use this initializer block.

I summarize briefly, static block runs when class loads, instance initialize block runs before constructor and if you have multiple constructor methods, they share this block.

After all this information, Let’s look at the codes together

Example-1

What is the output of the code below?

java-block-interview-question

We expect , static block runs firstly. 1. output:

Static Initialization Block

Later, we expect, runs main method. 2. output:

Main Class

Later, creating 1 instance of ExampleBlocks object. While creating instance, we expect, runs firstly instance initialize block. 3. output:

Instance Initialization Block

Later, creating another instance of ExampleBlocks object. While creating instance, we expect, runs firstly instance initialize block. 4. output:

Instance Initialization Block

Example-2

What is the output of the code below?

java-block-interview-question-2

Firstly, when we review code,2 static blocks, 2 initialize blocks, 1 constructor defined. In main class, creating 2 instance of ExampleBlocks object.

Firstly runs static block and also code runs from top. 1. Output:

1 — Static Block

Next output:

2 — Static Block

Next output:

Main Class

Later, creating ExampleBlocks instance (ins1) in main class, because of instance initialize blocks run before constructor method, we expect that output is instance block. But the code contains 2 initialize blocks. So first output is :

1 — Instance Block

Next output:

2 — Instance Block

After running initialize blocks, constructor block will run. Next output is:

Default Constructor

Now, it is critical point. Above output is for ins1 instance. The code contains one more ExampleBlocks instance. Same outputs should be for ins2 instance. OK. Next outputs are:

1 — Instance Block

2 — Instance Block

Default Constructor

Example-3

What is the output of the code below?

Now Let’s put OOP in the code :)

Firstly, looks a bit difficult. But now Let’s review the code together.

We have a ExampleBlocks class.This class has 1 static,2 instance initialize blocks and 1 constructor class.

SubClass1 extends ExampleBlocks. This class has 1 static, 1 instance initialize blocks and 1 constructor class.

SubClass4 extends SubClass1. This class has 1 static, 1 instance initialize blocks and 1 constructor class.

SubClass2 extends SubClass1. This class has 2 static, 2 instance initialize blocks and 2 constructor class.

Before talking about main method, let’s continue to review other class.

SubClass3 extends SubClass2. This class has 1 static, 1 instance initialize blocks and 1 constructor class.

ClassNotSub is not inherited. This class has 1 static, 1 instance initialize blocks.

Now let’s review main method. SubClass2 has main method. In main class, first output code, later creating an instance of SubClass2, later output code, creating two instances of ClassNotSub defined.

Now let’s review outputs :)

Firstly, where do we start? Of course, start point is static block. One question for you? From the parent to child or from the child to parent? You think, which class is loaded by classloader? Yes, parent inits firstly.

First output is for static blocks

1.) ExampleBlocks static init block

1.) SubClass1 static init block

1.) SubClass2 static init block

2.) SubClass2 static init block

Where is outputs for SubClass4 static block,SubClass3 static block and ClassNotSub static block? Now SubClass4, SubClass3 and ClassNotSub is not loaded yet. So this outputs of static blocks are not available.

Next output:

1) SubClass2 main method (main method inside)

Next output:

1.) ExampleBlocks Instance Init Block // (parent object’s initialize)

2.) ExampleBlocks Instance Init Block // (parent object’s initialize)

ExampleBlocks — No-Arg Constructor // (parent object’s constructor)

Next output:

1.) SubClass1 Instance Init Block // (parent object’s initialize)

SubClass1 — No-Arg Constructor // (parent object’s constructor)

Next output:

1.) SubClass2 Instance Init Block // this object’s initialize

2.) SubClass2 Instance Init Block // this object’s initialize

10

SubClass2 — No-Arg Constructor // this object’s constructor

Next output:

2) SubClass2 main method

Next output:

1.) SubClass3 static init block // SubClass3 load and initialize in the main method)

Note : SubClass3 extends SubClass2. But outputs of static blocks of parents don’t visible again because all static blocks of parents was already loaded.

Next output :

1.) ExampleBlocks Instance Init Block (parent object instance initialize)

2.) ExampleBlocks Instance Init Block (parent object instance initialize)

ExampleBlocks — No-Arg Constructor (parent object constructor)

1.) SubClass1 Instance Init Block (parent object instance initialize)

SubClass1 — No-Arg Constructor (parent object constructor)

1.) SubClass2 Instance Init Block (parent object instance initialize)

2.) SubClass2 Instance Init Block (parent object instance initialize)

10

SubClass2 — No-Arg Constructor (parent object constructor)

Next output:

1.) SubClass3 Instance Init Block (this class instance initialize)

SubClass3 — No-Arg Constructor

Next output:

1.) ClassNotSub static init block (ClassNotSub instance loaded )

2.) ClassNotSub Instance Init Block (ClassNotSub instance initialize )

And finish 😊

Where is output of static block for SubClass4? When we review the code, we didn’t create SubClass4 instance.So no output is for SubClass4.

I hope, I was able to explain the subject well with examples
Have a good days

--

--

Serdar A.

Senior Software Developer & Architect at Havelsan Github: https://github.com/serdaralkancode #Java & #Spring & #BigData & #React & #Microservice