Synopsis: | Avoid double brace initialization |
Language: | Java |
Severity Level: | 5 |
Category: | Optimization |
Description: |
Double brace initialization is a pattern to initialize objects, such as collections, concisely. However, it implicitly generates a new .class file, and the object holds a strong reference to the enclosing object. For those reasons, it is preferable to initialize the object normally, even though it’s verbose.Set<String> countries = new HashSet<String>() { // violation { add("India"); add("USSR"); add("USA"); } }; |