Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this quick tutorial, we’ll show how double braces can be used for creating and initializing objects in a single Java expression.

We’ll also look at why this technique can be considered an anti-pattern.

2. Standard Approach

Normally we initialize and populate a set of countries as follows:

@Test
public void whenInitializeSetWithoutDoubleBraces_containsElements() {
    Set<String> countries = new HashSet<String>();                
    countries.add("India");
    countries.add("USSR");
    countries.add("USA");
 
    assertTrue(countries.contains("India"));
}

As can be seen from above example, we are doing the following:

  1. Create an instance of HashSet
  2. Add countries to the HashSet
  3. Finally, we assert whether the country is present in the HashSet

3. Using Double Brace

However, we can actually combine the creation and initialization in a single statement; this is where we make use of double braces:

@Test
public void whenInitializeSetWithDoubleBraces_containsElements() {
    Set<String> countries = new HashSet<String>() {
        {
           add("India");
           add("USSR");
           add("USA");
        }
    };
 
    assertTrue(countries.contains("India"));
}

As can be seen from above example, we are:

  1. Creating an anonymous inner class which extends HashSet
  2. Providing an instance initialization block which invokes the add method and adds the country name to the HashSet
  3. Finally, we can assert whether the country is present in the HashSet

4. Advantages of Using Double Braces

There are some simple advantages of using double braces:

  • Fewer lines of code compared to the native way of creation and initialisation
  • The code is more readable
  • Creation initialization is done in the same expression

5. Disadvantages of Using Double Braces

Disadvantages of using double braces are:

  • Obscure, not widely known way to do the initialization
  • It creates an extra class every time we use it
  • Doesn’t support the use of the “diamond operator” – a feature introduced in Java 7
  • Doesn’t work if the class we are trying to extend is marked final
  • Holds a hidden reference to the enclosing instance, which may cause memory leaks

It’s due to these disadvantages that double brace initialization is considered as an anti-pattern.

6. Alternatives

6.1. Stream Factory Methods

Instead, we can make good use of the new Java 8 Stream API to initialize our Set:

@Test
public void whenInitializeUnmodifiableSetWithDoubleBrace_containsElements() {
    Set<String> countries = Stream.of("India", "USSR", "USA")
      .collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
 
    assertTrue(countries.contains("India"));
}

6.2. Java 9 Collections Factory Methods

Also, Java 9 will bring a set of useful factory methods that will make the following possible:

List<String> list = List.of("India", "USSR", "USA");
Set<String> set = Set.of("India", "USSR", "USA");

You can read more about this in this article.

7. Conclusion

In this concise tutorial, we discussed the usage of double braces along with its advantages and disadvantages.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW