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
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.