File: DoubleBraceInitialization.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,644 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
The [double-brace initialization pattern][dbi] should be avoided—especially in
non-static contexts.

The double-brace pattern uses an instance-initializer in an anonymous inner
class to express the initialization of a class (often a collection) in a single
step.

Inner classes in a non-static context are terrific sources of memory leaks! If
you pass the collection somewhere that retains it, the entire instance you
created it from can no longer be garbage collected. Even if it is completely
unreachable. And if someone serializes the map? Yep, the entire creating
instance goes along for the ride (or if that fails, serializing the map fails,
which is also awfully strange). All this is completely nonobvious.

Luckily, there are more readable and more performant alternatives in the factory
methods and builders for `ImmutableList`, `ImmutableSet`, and `ImmutableMap`.

The `List.of`, `Set.of`, and `Map.of` static factories
[added in Java 9](http://openjdk.java.net/jeps/269) are also a good choice.

That is, prefer this:

```java
ImmutableList.of("Denmark", "Norway", "Sweden");
```

Not this:

```java
new ArrayList<>() {
  {
    add("Denmark");
    add("Norway");
    add("Sweden");
  }
};
```

TIP: Neither the guava immutable collections nor the static factory methods
added in a JDK 9 support `null` elements. The double-brace pattern is still best
avoided for collections that contain null. Consider using `Arrays.asList` to
initialize `List`s and `Set`s with `null` values, and refactoring `Map`
initializers into a helper method.

[dbi]: https://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-in-java