Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 216619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:34:53+00:00 2026-05-11T18:34:53+00:00

In Hidden Features of Java the top answer mentions Double Brace Initialization , with

  • 0

In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax:

Set<String> flavors = new HashSet<String>() {{
    add("vanilla");
    add("strawberry");
    add("chocolate");
    add("butter pecan");
}};

This idiom creates an anonymous inner class with just an instance initializer in it, which “can use any […] methods in the containing scope”.

Main question: Is this as inefficient as it sounds? Should its use be limited to one-off initializations? (And of course showing off!)

Second question: The new HashSet must be the “this” used in the instance initializer … can anyone shed light on the mechanism?

Third question: Is this idiom too obscure to use in production code?

Summary: Very, very nice answers, thanks everyone. On question (3), people felt the syntax should be clear (though I’d recommend an occasional comment, especially if your code will pass on to developers who may not be familiar with it).

On question (1), the generated code should run quickly. The extra .class files do cause jar file clutter, and slow program startup slightly (thanks to @coobird for measuring that). @Thilo pointed out that garbage collection can be affected, and the memory cost for the extra loaded classes may be a factor in some cases.

Question (2) turned out to be most interesting to me. If I understand the answers, what’s happening in DBI is that the anonymous inner class extends the class of the object being constructed by the new operator, and hence has a “this” value referencing the instance being constructed. Very neat.

Overall, DBI strikes me as something of an intellectual curiousity. Coobird and others point out you can achieve the same effect with Arrays.asList, varargs methods, Google Collections, and the proposed Java 7 Collection literals. Newer JVM languages like Scala, JRuby, and Groovy also offer concise notations for list construction, and interoperate well with Java. Given that DBI clutters up the classpath, slows down class loading a bit, and makes the code a tad more obscure, I’d probably shy away from it. However, I plan to spring this on a friend who’s just gotten his SCJP and loves good natured jousts about Java semantics! 😉 Thanks everyone!

7/2017: Baeldung has a good summary of double brace initialization and considers it an anti-pattern.

12/2017: @Basil Bourque notes that in the new Java 9 you can say:

Set<String> flavors = Set.of("vanilla", "strawberry", "chocolate", "butter pecan");

That’s for sure the way to go. If you’re stuck with an earlier version, take a look at Google Collections’ ImmutableSet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:34:53+00:00Added an answer on May 11, 2026 at 6:34 pm

    Here’s the problem when I get too carried away with anonymous inner classes:

    2009/05/27  16:35             1,602 DemoApp2$1.class
    2009/05/27  16:35             1,976 DemoApp2$10.class
    2009/05/27  16:35             1,919 DemoApp2$11.class
    2009/05/27  16:35             2,404 DemoApp2$12.class
    2009/05/27  16:35             1,197 DemoApp2$13.class
    
    /* snip */
    
    2009/05/27  16:35             1,953 DemoApp2$30.class
    2009/05/27  16:35             1,910 DemoApp2$31.class
    2009/05/27  16:35             2,007 DemoApp2$32.class
    2009/05/27  16:35               926 DemoApp2$33$1$1.class
    2009/05/27  16:35             4,104 DemoApp2$33$1.class
    2009/05/27  16:35             2,849 DemoApp2$33.class
    2009/05/27  16:35               926 DemoApp2$34$1$1.class
    2009/05/27  16:35             4,234 DemoApp2$34$1.class
    2009/05/27  16:35             2,849 DemoApp2$34.class
    
    /* snip */
    
    2009/05/27  16:35               614 DemoApp2$40.class
    2009/05/27  16:35             2,344 DemoApp2$5.class
    2009/05/27  16:35             1,551 DemoApp2$6.class
    2009/05/27  16:35             1,604 DemoApp2$7.class
    2009/05/27  16:35             1,809 DemoApp2$8.class
    2009/05/27  16:35             2,022 DemoApp2$9.class
    

    These are all classes which were generated when I was making a simple application, and used copious amounts of anonymous inner classes — each class will be compiled into a separate class file.

    The “double brace initialization”, as already mentioned, is an anonymous inner class with an instance initialization block, which means that a new class is created for each “initialization”, all for the purpose of usually making a single object.

    Considering that the Java Virtual Machine will need to read all those classes when using them, that can lead to some time in the bytecode verfication process and such. Not to mention the increase in the needed disk space in order to store all those class files.

    It seems as if there is a bit of overhead when utilizing double-brace initialization, so it’s probably not such a good idea to go too overboard with it. But as Eddie has noted in the comments, it’s not possible to be absolutely sure of the impact.


    Just for reference, double brace initialization is the following:

    List<String> list = new ArrayList<String>() {{
        add("Hello");
        add("World!");
    }};
    

    It looks like a “hidden” feature of Java, but it is just a rewrite of:

    List<String> list = new ArrayList<String>() {
    
        // Instance initialization block
        {
            add("Hello");
            add("World!");
        }
    };
    

    So it’s basically a instance initialization block that is part of an anonymous inner class.


    Joshua Bloch’s Collection Literals proposal for Project Coin was along the lines of:

    List<Integer> intList = [1, 2, 3, 4];
    
    Set<String> strSet = {"Apple", "Banana", "Cactus"};
    
    Map<String, Integer> truthMap = { "answer" : 42 };
    

    Sadly, it didn’t make its way into neither Java 7 nor 8 and was shelved indefinitely.


    Experiment

    Here’s the simple experiment I’ve tested — make 1000 ArrayLists with the elements "Hello" and "World!" added to them via the add method, using the two methods:

    Method 1: Double Brace Initialization

    List<String> l = new ArrayList<String>() {{
      add("Hello");
      add("World!");
    }};
    

    Method 2: Instantiate an ArrayList and add

    List<String> l = new ArrayList<String>();
    l.add("Hello");
    l.add("World!");
    

    I created a simple program to write out a Java source file to perform 1000 initializations using the two methods:

    Test 1:

    class Test1 {
      public static void main(String[] s) {
        long st = System.currentTimeMillis();
    
        List<String> l0 = new ArrayList<String>() {{
          add("Hello");
          add("World!");
        }};
    
        List<String> l1 = new ArrayList<String>() {{
          add("Hello");
          add("World!");
        }};
    
        /* snip */
    
        List<String> l999 = new ArrayList<String>() {{
          add("Hello");
          add("World!");
        }};
    
        System.out.println(System.currentTimeMillis() - st);
      }
    }
    

    Test 2:

    class Test2 {
      public static void main(String[] s) {
        long st = System.currentTimeMillis();
    
        List<String> l0 = new ArrayList<String>();
        l0.add("Hello");
        l0.add("World!");
    
        List<String> l1 = new ArrayList<String>();
        l1.add("Hello");
        l1.add("World!");
    
        /* snip */
    
        List<String> l999 = new ArrayList<String>();
        l999.add("Hello");
        l999.add("World!");
    
        System.out.println(System.currentTimeMillis() - st);
      }
    }
    

    Please note, that the elapsed time to initialize the 1000 ArrayLists and the 1000 anonymous inner classes extending ArrayList is checked using the System.currentTimeMillis, so the timer does not have a very high resolution. On my Windows system, the resolution is around 15-16 milliseconds.

    The results for 10 runs of the two tests were the following:

    Test1 Times (ms)           Test2 Times (ms)
    ----------------           ----------------
               187                          0
               203                          0
               203                          0
               188                          0
               188                          0
               187                          0
               203                          0
               188                          0
               188                          0
               203                          0
    

    As can be seen, the double brace initialization has a noticeable execution time of around 190 ms.

    Meanwhile, the ArrayList initialization execution time came out to be 0 ms. Of course, the timer resolution should be taken into account, but it is likely to be under 15 ms.

    So, there seems to be a noticeable difference in the execution time of the two methods. It does appear that there is indeed some overhead in the two initialization methods.

    And yes, there were 1000 .class files generated by compiling the Test1 double brace initialization test program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 105k
  • Answers 105k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you're interested into the number of processors available to… May 11, 2026 at 8:40 pm
  • Editorial Team
    Editorial Team added an answer As the error states, you can either do a filegroup… May 11, 2026 at 8:40 pm
  • Editorial Team
    Editorial Team added an answer For erase(key), the standard says that all elements with value… May 11, 2026 at 8:40 pm

Related Questions

It seems like Groovy was forgotten in this thread so I'll just ask the
Is it possible to programmatically start an application from Java and then send commands
This came up in Hidden features of Python , but I can't see good
I was browsing the Hidden Features of C# question and thought I would try
What are most important things you know about generics: hidden features, common mistakes, best

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.