Use of double braces in Java Programming
2 min readAug 8, 2022
We can use double braces in Java program when we need to initialise a class without creating an object and make use of the functions in it. This is a concept of creating an anonymous class inside the parent class.
new Parent() {{ }}
The first braces set create an anonymous class and second braces set creates the content inside the class. The methods that are called inside the anonymous class refer to the main class by a hidden “this” keyword. So we doesn’t need to mention the keyword.
Usages:
- No need of any object creation, and many anonymous class can be created inside an anonymous class
- This concept can be used for quick development purpose.
Concerns:
- There could be a possible of memory leakage as the base class holds all the memory of the child anonymous classes in it.
- Garbage value collection cannot be achieved efficiently due to heavy resources in it
- Creating many anonymous class, in parallel creates many class files with ‘$’ followed by number