|
1 |
| -Pattern: Use of double-brace initialization |
2 |
| - |
3 |
| -Issue: - |
4 |
| - |
5 |
| -## Description |
6 |
| - |
7 |
| -Double-brace initialization can be considered as an anti-pattern, as it unnecessarily creates subclasses and also might break `equals` checks that most often consider instances to be non-equal if the class is not the same to ensure symmetric behavior of the `equals` method as required by its contract. |
8 |
| - |
9 |
| -## Examples |
10 |
| - |
11 |
| -```java |
12 |
| -// will fail |
13 |
| -List<?> list = new ArrayList<Object>() { |
14 |
| - { |
15 |
| - add(null); |
16 |
| - } |
17 |
| -}; |
18 |
| -// will fail |
19 |
| -list = new ArrayList<Object>() { |
20 |
| - { |
21 |
| - add(null); |
22 |
| - } |
23 |
| - { |
24 |
| - add(null); |
25 |
| - } |
26 |
| -}; |
27 |
| -// will fail |
28 |
| -list = new ArrayList<Object>() { |
29 |
| - // foo |
30 |
| - { |
31 |
| - add(null); |
32 |
| - } |
33 |
| -}; |
34 |
| -// will fail |
35 |
| -list = new ArrayList<Object>() { |
36 |
| - { |
37 |
| - add(null); |
38 |
| - } // foo |
39 |
| -}; |
40 |
| -// will fail |
41 |
| -list = new ArrayList<Object>() { |
42 |
| - { |
43 |
| - add(null); |
44 |
| - } |
45 |
| - // foo |
46 |
| -}; |
47 |
| -// will fail |
48 |
| -list = new ArrayList<Object>() { |
49 |
| - /* foo */ |
50 |
| - { |
51 |
| - add(null); |
52 |
| - } |
53 |
| -}; |
54 |
| -// will fail |
55 |
| -list = new ArrayList<Object>() { |
56 |
| - { |
57 |
| - add(null); |
58 |
| - } |
59 |
| - /* foo */ |
60 |
| -}; |
61 |
| -// will fail |
62 |
| -list = new ArrayList<Object>() { |
63 |
| - { |
64 |
| - add(null); |
65 |
| - } /* foo */ |
66 |
| -}; |
67 |
| -// will not fail |
68 |
| -list = new ArrayList<Object>() { |
69 |
| - { |
70 |
| - add(null); |
71 |
| - } |
72 |
| - |
73 |
| - public void foo() { |
74 |
| - } |
75 |
| -}; |
76 |
| -// will not fail |
77 |
| -list = new ArrayList<Object>() { |
78 |
| - private Object o; |
79 |
| - { |
80 |
| - add(null); |
81 |
| - } |
82 |
| -}; |
83 |
| -``` |
84 |
| - |
85 |
| -## Further Reading |
86 |
| - |
87 |
| -* [checkstyle - AvoidDoubleBraceInitialization](https://checkstyle.sourceforge.io/config_misc.html#AvoidDoubleBraceInitialization) |
| 1 | +Pattern: Use of double-brace initialization |
| 2 | + |
| 3 | +Issue: - |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +Double-brace initialization can be considered as an anti-pattern, as it unnecessarily creates subclasses and also might break `equals` checks that most often consider instances to be non-equal if the class is not the same to ensure symmetric behavior of the `equals` method as required by its contract. |
| 8 | + |
| 9 | +## Examples |
| 10 | + |
| 11 | +```java |
| 12 | +// will fail |
| 13 | +List<?> list = new ArrayList<Object>() { |
| 14 | + { |
| 15 | + add(null); |
| 16 | + } |
| 17 | +}; |
| 18 | +// will fail |
| 19 | +list = new ArrayList<Object>() { |
| 20 | + { |
| 21 | + add(null); |
| 22 | + } |
| 23 | + { |
| 24 | + add(null); |
| 25 | + } |
| 26 | +}; |
| 27 | +// will fail |
| 28 | +list = new ArrayList<Object>() { |
| 29 | + // foo |
| 30 | + { |
| 31 | + add(null); |
| 32 | + } |
| 33 | +}; |
| 34 | +// will fail |
| 35 | +list = new ArrayList<Object>() { |
| 36 | + { |
| 37 | + add(null); |
| 38 | + } // foo |
| 39 | +}; |
| 40 | +// will fail |
| 41 | +list = new ArrayList<Object>() { |
| 42 | + { |
| 43 | + add(null); |
| 44 | + } |
| 45 | + // foo |
| 46 | +}; |
| 47 | +// will fail |
| 48 | +list = new ArrayList<Object>() { |
| 49 | + /* foo */ |
| 50 | + { |
| 51 | + add(null); |
| 52 | + } |
| 53 | +}; |
| 54 | +// will fail |
| 55 | +list = new ArrayList<Object>() { |
| 56 | + { |
| 57 | + add(null); |
| 58 | + } |
| 59 | + /* foo */ |
| 60 | +}; |
| 61 | +// will fail |
| 62 | +list = new ArrayList<Object>() { |
| 63 | + { |
| 64 | + add(null); |
| 65 | + } /* foo */ |
| 66 | +}; |
| 67 | +// will not fail |
| 68 | +list = new ArrayList<Object>() { |
| 69 | + { |
| 70 | + add(null); |
| 71 | + } |
| 72 | + |
| 73 | + public void foo() { |
| 74 | + } |
| 75 | +}; |
| 76 | +// will not fail |
| 77 | +list = new ArrayList<Object>() { |
| 78 | + private Object o; |
| 79 | + { |
| 80 | + add(null); |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +## Further Reading |
| 86 | + |
| 87 | +* [checkstyle - AvoidDoubleBraceInitialization](https://checkstyle.sourceforge.io/checks/coding/avoiddoublebraceinitialization.html#AvoidDoubleBraceInitialization) |
0 commit comments