Java's Double-Brace initialization creates Objects using anonymous classes.
Questions tagged [double-brace-initialize]
16 questions
391
votes
13 answers
What is Double Brace initialization in Java?
What is Double Brace initialization syntax ({{ ... }}) in Java?

Saurabh Gokhale
- 53,625
- 36
- 139
- 164
47
votes
4 answers
Meaning of new Class(...){{...}} initialization idiom
What does {{ ... }} block mean in the following code?
class X {
private Y var1;
private X() {
Z context = new Z(new SystemThreadPool()) {{
var1 = new Y();
}};
}
}

Victor Sorokin
- 11,878
- 2
- 35
- 51
15
votes
4 answers
Java double brace initialization works always?
I know that this code:
Set set = new HashSet() {{
add("test1");
add("test2");
}};
is really:
Set set = new HashSet() {
{//initializer
add("test1");
add("test2");
}
};
The initializer block is being…

user926958
- 9,355
- 7
- 28
- 33
8
votes
2 answers
Double brace initialization with nested collections
I know I can declare and initialize a List using double braces:
// (1)
List

Arvind Vishwakarma
- 221
- 4
- 18
3
votes
2 answers
Java Double brace initialization
I have refactored the following object initialization:
Req r = new Req();
r.set_f1("A");
r.set_f2(123);
r.set_f3(123.456);
Into:
Req r = new Req() {{
set_f1("A");
set_f2(123);
set_f3(123.456)
}};
The second sample raises the following…

Adam Matan
- 128,757
- 147
- 397
- 562
3
votes
1 answer
Alternative to double brace initialization
I have a nested collection in the form:
HashMap>> errorList;
Now I initialize it inline using double braces like this
errorList.put(tempName, new HashMap>() {{
put("upl", new…

kaattaalan
- 61
- 7
3
votes
1 answer
double brace initialization and "kind of" static anonymous class
Sometimes for testing I use quick "double-brace" initialization which creates anonymous nested class in Outer class, for example:
static final Set sSet1 = new HashSet() {
{
add("string1");
add("string2");
…

kiruwka
- 9,250
- 4
- 30
- 41
2
votes
2 answers
Java double brace initialization vs lambda
If you search 'java double brace' you'll find strong arguments against using it.
Every time someone uses double brace initialisation, a kitten gets killed.
https://stackoverflow.com/a/27521360/555631
The arguments are you're creating way too…

everett1992
- 2,351
- 3
- 27
- 38
2
votes
2 answers
method parameters in double brace initialization?
I'm creating a HashMap inline with double braces inside a function:
public void myFunction(String key, String value) {
myOtherFunction(
new JSONSerializer().serialize(
new HashMap() {{
…
user1382306
1
vote
1 answer
Can an object referencing itself inside double-brace initialization not give a NPE?
I have this little piece of code here, which always will throw a NPE:
public class Test1 {
private final static Object OBJECT = new Object() {{
System.out.println("OBJECT.toString() = " + OBJECT.toString());
}};
public static…

skiwi
- 66,971
- 31
- 131
- 216
1
vote
2 answers
Double brace initialization in JRuby
I am trying out JRuby, and I was trying to figure out how to use Java's double brace initialization. However, it is not that apparent how the syntax would be.
To keep this example simple, the below Java code would create a list containing an…

whirlwin
- 16,044
- 17
- 67
- 98
0
votes
1 answer
How do I fix these double braces
Hello I am trying to tidy up some code and I have stumbled across some double braces. I realize that this is a sin and that I should fix this. However, I have no idea where to begin. Can anyone help?
private SelectItem notifyTypeItem = new…

McGhee
- 13
- 5
0
votes
0 answers
Java instanciation syntax explanation
Reading a Java project on GitHub, I found a code syntax that I have never seen before and I would some explanation, please. Here is the code:
Book book = new BookBuilder() {
{
description("blabla");
author("blabla");
…

gWombat
- 517
- 2
- 11
0
votes
1 answer
Regex match double curly braces nested
would regex matches while/loop double curly braces contains double curly braces?
{{var doc-title}}
…

meYnot
- 343
- 2
- 6
- 13
0
votes
2 answers
Concatenate lists with double brace initialization
I would like to concatenate two array lists as it shows in the answer:
final List l1 = Lists.newArrayList(...);
final List l2 = Lists.newArrayList(...);
List l = new ArrayList() { { addAll(l1); addAll(l2); } };
Is…

Loom
- 9,768
- 22
- 60
- 112