HamedFathi
HamedFathi

Reputation: 3969

When use Braces block in Java like this?

enter image description here

please see above picture (from Wrox Beginning Spring book)

I have this question that what is { } ?

Is constructor ? Is functional block ? Is block of "accountsMap" ? What is it ?

please explain this feature in java ? what is the name of this feature ?

Upvotes: 7

Views: 225

Answers (2)

Amit.rk3
Amit.rk3

Reputation: 2417

That's an instance initializer block. Whenever a new instance of any class is created, code inside these braces gets executed prior to constructor invokation. http://www.javatpoint.com/instance-initializer-block

General sequence of blocks execution.

1- Static initializer block (Invoked when class is loaded)

And when new instance is created

2- Instance initializer block
3 - Constructor

Upvotes: 1

Eran
Eran

Reputation: 393781

It's an instance initializer block which gets executed each time you create an instance of the class, no matter which constructor is used. It is executed prior to the code of the executed constructor. See JLS 8.6 for more details.

Upvotes: 10

Related Questions