Questions tagged [double-brace-initialize]

Java's Double-Brace initialization creates Objects using anonymous classes.

See http://www.c2.com/cgi/wiki?DoubleBraceInitialization

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 myList = new ArrayList(){{ add("Object1"); add("Object2"); }}; But I want a List of >: // (2) List> myList =…
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…
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…
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}}</div> </head> <body> <div class="container"> <div class="row" {{while products}}> …</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/php" class="post-tag grid--cell" title="show questions tagged 'php'" rel="tag">php</a> <a href="../../questions/tagged/regex" class="post-tag grid--cell" title="show questions tagged 'regex'" rel="tag">regex</a> <a href="../../questions/tagged/double-brace-initialize" class="post-tag grid--cell" title="show questions tagged 'double-brace-initialize'" rel="tag">double-brace-initialize</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Feb 19 '16 at 12:59">asked Feb 19 '16 at 12:59</time> <a href="../../users/1289820/meynot" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1289820.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="meYnot" /> </a> <div class="s-user-card--info"> <a href="../../users/1289820/meynot" class="s-user-card--link">meYnot</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">343</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="6 silver badges">6</li> <li class="s-award-bling s-award-bling__bronze" title="13 bronze badges">13</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="mln24"> <div class="question-summary" id="question-summary-30845983"> <div class="statscontainer"> <div class="stats"> <div class="vote"> <div class="votes"> <span class="vote-count-post"><strong>0</strong></span> <div class="viewcount">votes</div> </div> </div> <div class="status "> <strong>2</strong> answers </div> </div> </div> <div class="summary"> <h3><a href="../../questions/30845983/concatenate-lists-with-double-brace-initialization" class="question-hyperlink">Concatenate lists with double brace initialization</a></h3> <div class="excerpt">I would like to concatenate two array lists as it shows in the answer: final List<String> l1 = Lists.newArrayList(...); final List<String> l2 = Lists.newArrayList(...); List<String> l = new ArrayList<String>() { { addAll(l1); addAll(l2); } }; Is…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/java" class="post-tag grid--cell" title="show questions tagged 'java'" rel="tag">java</a> <a href="../../questions/tagged/arraylist" class="post-tag grid--cell" title="show questions tagged 'arraylist'" rel="tag">arraylist</a> <a href="../../questions/tagged/double-brace-initialize" class="post-tag grid--cell" title="show questions tagged 'double-brace-initialize'" rel="tag">double-brace-initialize</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Jun 15 '15 at 13:06">asked Jun 15 '15 at 13:06</time> <a href="../../users/723845/loom" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/723845.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Loom" /> </a> <div class="s-user-card--info"> <a href="../../users/723845/loom" class="s-user-card--link">Loom</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">9,768</li> <li class="s-award-bling s-award-bling__gold" title="22 gold badges">22</li> <li class="s-award-bling s-award-bling__silver" title="60 silver badges">60</li> <li class="s-award-bling s-award-bling__bronze" title="112 bronze badges">112</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="s-pagination pager fr"> <div class="s-pagination--item is-selected">1</div> <a class="s-pagination--item" href="../../questions/tagged/double-brace-initialize_page=2" rel="" title="Go to page 2">2</a> <a class="s-pagination--item" href="../../questions/tagged/double-brace-initialize_page=2" rel="next" title="Go to page 2"> Next</a> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>