Meaning of new Class(…){{…}} initialization idiom [duplicate]

It’s called double curly brace initialization. (EDIT: Link removed, archived here) It means you’re creating an anonymous subclass and the code within the double braces is basically a constructor. It’s often used to add contents to collections because Java’s syntax for creating what are essentially collection constants is somewhat awkward. So you might do: List<String> … Read more

What is Double Brace initialization in Java?

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); }}; Note that an effect of using this double brace initialisation is that you’re creating anonymous inner classes. The created class has an … Read more