What actually means using brackets in brackets while initializing e.g. new List ?
new ArrayList<String>()
{
{
add("A");
}
};
Does it mean that after creating new reference method is invoking ?
This is a clever idiom for creating and initializing a collection.
What is actually happening is that you are creating an instance of an anonymous subclass of ArrayList
which has an instance initializer block that calls add
on list instance to populate it. The inner {...}
is the instance initializer block.
A similar trick can be used to create pre-initialized maps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With