Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

What is Double Brace initialization syntax ({{ ... }}) in Java?

1 Answer

0 votes
by (46k points)
edited by

Double brace initialization generates an unknown class received from the designated class (the outside braces) and gives an initializer segment inside that class (the internal braces). e.g.

new ArrayList<Integer>() {{

   add(1);

   add(2);

}};

Note that the impact of practicing this double brace initialization is that you're planning anonymous inner classes. The generated class has an absolute this pointer to the enclosing outer class. 

Want to learn Java from scratch? Here's is the right video for you on Java provided by Intellipaat

Whilst not usually a difficulty, it can create anxiety in some situations e.g. when serializing or garbage collecting, and it's deserving being informed of this.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 30, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...