Java9 Diamond operator anonymous inner class example

In this blog post, we’ll cover the Java 9 feature: Diamond Operator Improvement, which now allows anonymous inner classes. Let’s see an example.

Let’s see how a diamond operator declares in java 7,8,9 versions

Java 7 Diamond Operator Example

We can declare the diamond operator as follows

List<String> listjava6 = new ArrayList<String>();  //  Before java7 ie Java6 or less
List<String> listjava7 = new ArrayList<>();   //  with java7

On the left side, we’re using generics with a data type of String. On the right side of the equal operator is a raw type (ArrayList), which is inferred from the left side type. We can apply diamond operators to inferred types of denotable types like Integer and String, which can be used with generics in Java.

Java 8 Diamond Operator Anonymous Inner Class Example

Let’s create an abstract Message class

abstract class Message<T> {
    public T text;
    public Message(T text) {
        this.text = text;
    }
    abstract void getMessage();
}

Now, let’s implement the abstract class with Java 8.

Message<String> stringMessage = new Message<String>("one") {
   @Override
   public void getMessage() {
    System.out.println(text);
   }
};
stringMessage.getMessage();

This is a valid code, and the diamond operator is not used here.

How do we write code with the Diamond Operator?

Message<String> stringMessage1 = new Message<>("one") {
   @Override
   public void getMessage() {
    System.out.println(text);
   }
};
// or
Message<?> stringMessage1 = new Message<>("one") {
   @Override
   public void getMessage() {
    System.out.println(text);
   }
};

This code is simpler and more readable. However, in Java 8 or earlier, the compiler throws an error for the Diamond Operator with anonymous inner classes.

compilation error '<>' cannot be used with anonymous classes. In java8 or before it, the Compiler throws an error for _Diamond Operator anonymous inner classes.

Java 9 allows using the diamond operator with anonymous inner classes.

Java 9 Diamond Operator Program Example

Diamond operators can now be used with inner classes as well

Message<?> stringMessage1 = new Message<>("one") {
   @Override
   public void getMessage() {
    System.out.println(text);
   }
};
// or
Message<String> stringMessage1 = new Message<>("one") {
   @Override
   public void getMessage() {
    System.out.println(text);
   }
};

Java Diamond Operator Anonymous Inner Classes Example

Anonymous inner classes are now compatible with the diamond operator

public class Summary {
    public static void main(String[] args) {
        Message<String> stringMessage = new Message<String>("one") {
            @Override
            public void getMessage() {
                System.out.println(text);
            }
        };
        stringMessage.getMessage();

        Message<String> stringMessage1 = new Message<>("two") {
            @Override
            public void getMessage() {
                System.out.println(text);
            }
        };
        stringMessage1.getMessage();
    }
}

abstract class Message<T> {
    public T text;

    public Message(T text) {
        this.text = text;
    }

    abstract void getMessage();
}

Output:

one
two

Though lambda expressions introduced in Java 8 have reduced the usage of anonymous inner classes, they still enhance the readability of the code and can be used if necessary.