What is Double Brace initialization in Java?

Technology CommunityCategory: JavaWhat is Double Brace initialization in Java?
VietMX Staff asked 3 years ago

Double brace initialisation creates an anonymous class derived from the specified class (the outer braces), and provides an initialiser block within that class (the inner braces). e.g.

new ArrayList<Integer>() {{
   add(1);
   add(2);
}};

However, I’m not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object — that just seems like a little bit overkill to me.