InterviewSolution
Saved Bookmarks
| 1. |
Explain the term “Double Brace Initialisation” in Java? |
|
Answer» This is a convenient means of initializing any collections in Java. Consider the below example. import java.util.HashSet;import java.util.SET; public class IBDoubleBraceDemo{ public static void main(String[] args){ Set<String> stringSets = NEW HashSet<String>() { { add("set1"); add("set2"); add("set3"); } }; doSomething(stringSets); } private static void doSomething(Set<String> stringSets){ System.out.println(stringSets); }}In the above example, we SEE that the stringSets were initialized by using double braces.
Care should be taken while initializing through this method as the method involves the creation of anonymous inner classes which can cause PROBLEMS during the garbage collection or serialization processes and may ALSO result in memory leaks. |
|