Java has a feature called double brace initialization which allows us to initialize the objects in double brace syntax. For ex. We initialize list in java as below:
List<String> states = new ArrayList<String>(); states.add("Orissa"); states.add("AP"); states.add("Maharastra");
But using Double brace initialization we can initialize the list in the following way:
List<String> states = new ArrayList<String>() {{ add("Orissa "); add("AP "); add("Maharastra "); }};
The first brace creates a new AnonymousInnerClass, and the second declares an instance initializer block that is run when the anonymous inner class is instantiated. This type of initializer block is called an “instance initializer”, as declaration is done within the instance scope of the class.
double brace initialization works only for non-final classes. Because it needs to create an anonymous subclass and we can’t create subclass of a final class.