Double Brace Initialization in JavaDouble brace initialization is a technique used in Java to initialize an instance of a class and provide initial values for its fields in a concise and convenient manner. It involves the use of nested curly braces within the instantiation code block. Although the approach can be useful in certain scenarios and also it is important to understand its implications and limitations. The double brace initialization syntax involves creating an anonymous inner class and utilizing an instance initializer block within it. Syntax:The outer new ClassName() creates an anonymous inner class based on ClassName. The double braces {{ ... }} define the instance initializer block. The block is executed during the creation of the anonymous inner class. Inside the instance initializer block, you can write initialization code, such as setting default values or invoking methods. The double brace initialization can be used for any class, including built-in classes, user-defined classes, or even anonymous classes. Approach 1: Standard methodWe all are aware of the standard method that is written without using the double brace in which are going to use the single braces for write the functions and statements in it. It is represented as {….}. ALGORITHM:Step 1: Start the program execution in the main method. Step 2: Create an empty HashSet of strings named stringSet. Step 3: Add three elements ("cat", "dog", and "lion") to the stringSet using the add method. Step 4: Call the useMethodIn helper method, passing the stringSet as an argument. Step 5: Inside the useMethodIn method: Step 5.1: Receive the stringSet parameter. Step 5.2: Print all elements of the set passed as the parameter using the System.out.println method. Step 6: The program execution ends. Implementation:The implementation of the above steps given below FileName: SomeClass.java Output: [cat, dog, lion] Approach 2: Double Brace InitializationThe double brace initialization technique provides a compact and inline way to initialize fields directly within the instantiation code block. It involves using nested braces to define the field values. ALGORITHM:Step 1: Start the program execution in the main method. Step 2: Create an empty HashSet named st using double brace initialization. Step 2.1: Inside the double braces, add three elements ("cat", "dog", and "lion") to the HashSet using the add method. Step 3: Call the useMethodIn helper method, passing the st HashSet as an argument. Step 4: Inside the useMethodIn method: Step 4.1: Receive the st parameter of type Set<String>. Step 4.2: Print all elements of the Set passed as the parameter using the System.out.println method. Step 5: The program execution ends. Implementation:The implementation of the above steps given below FileName: SomeClass.java Output: [cat, dog, lion] Advantages:
Disadvantages:
Next TopicFunctional Interface in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India