Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When use Braces block in Java like this?

Tags:

java

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 ?

like image 531
HamedFathi Avatar asked Jul 18 '15 08:07

HamedFathi


2 Answers

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.

like image 99
Eran Avatar answered Nov 15 '22 17:11

Eran


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

like image 23
Amit.rk3 Avatar answered Nov 15 '22 16:11

Amit.rk3