Diamond operator for Anonymous Inner Class with Examples in Java
Last Updated : 16 Jan, 2020
Prerequisite: Anonymous Inner Class Diamond Operator: Diamond operator was introduced in Java 7 as a new feature.The main purpose of the diamond operator is to simplify the use of generics when creating an object. It avoids unchecked warnings in a program and makes the program more readable. The diamond operator could not be used with Anonymous inner classes in JDK 7. In JDK 9, it can be used with the anonymous class as well to simplify code and improves readability. Before JDK 7, we have to create an object with Generic type on both side of the expression like:
// Here we mentioned the generic type // on both side of expression while creating object List<String> geeks = new ArrayList<String>();
When Diamond operator was introduced in Java 7, we can create the object without mentioning generic type on right side of expression like:
List<String> geeks = new ArrayList<>();
Problem with Diamond Operator in JDK 7? With the help of Diamond operator, we can create an object without mentioning the generic type on the right hand side of the expression. But the problem is it will only work with normal classes. Suppose you want to use the diamond operator for anonymous inner class then compiler will throw error message like below: Java // Program to illustrate the problem // while linking diamond operator // with an anonymous inner class abstract class Geeksforgeeks<T> { abstract T add(T num1, T num2); } public class Geeks { public static void main(String[] args) { Geeksforgeeks<Integer> obj = new Geeksforgeeks<>() { Integer add(Integer n1, Integer n2) { return (n1 + n2); } }; Integer result = obj.add(10, 20); System.out.println("Addition of two numbers: " + result); } }
Output:
prog.java:9: error: cannot infer type arguments for Geeksforgeeks Geeksforgeeks obj = new Geeksforgeeks () { ^ reason: cannot use '' with anonymous inner classes where T is a type-variable: T extends Object declared in class Geeksforgeeks 1 error
Java developer extended the feature of the diamond operator in JDK 9 by allowing the diamond operator to be used with anonymous inner classes too. If we run the above code with JDK 9, then code will run fine and we will generate the below output.
Output: Addition of two numbers: 30
Similar Reads
Class forName(String, boolean, ClassLoader) method in Java with Examples The forName(String, boolean, ClassLoader) method of java.lang.Class class is used to get the instance of this Class with the specified class name, using the specified class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.Syntax: pu
2 min read
Anonymous Inner Class in Java Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as o
7 min read
Different Types of Classes in Java with Examples A class is a user-defined blueprint or prototype from which objects are created. Â It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: Â Modifiers: A class can be public or has default access
11 min read
Class getSimpleName() method in Java with Examples The getSimpleName() method of java.lang.Class class is used to get the simple name of this class, as given in the sourcecode. The method returns the simple name of this class in the form of String. If this class is anonymous, then this method returns empty string.Syntax: public String getSimpleName(
2 min read
Java - Divide the Classes into Packages with Examples Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains vari
6 min read