List<String> countries = new ArrayList<String>() {{ add("India"); add("Switzerland"); add("Italy"); add("France"); add("Germany");}}; |
This type of initializer block is formally called an “instance initializer”, because it is declared withing the instance scope of the class — “static initializers” are a related concept where the keyword static is placed before the brace that starts the block, and which is executed at the class level as soon as the classloader completes loading the class . The initializer block can use any methods, fields and final variables available in the containing scope, but one has to be wary of the fact that initializers are run before constructors.
Read full article from Double Brace Initialization in Java! Java Initialization technique
No comments:
Post a Comment