Java9 Diamond operator anonymous inner class example

In this blog post, Cover the _Java9 features Diamond Operator Improvement for allowing anonymous inner class with an example.

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

java7 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

Left side is using Generics with a data type of String, Right side of the equal operator is raw type ie. ArrayList and it inferred from left side type. We can apply diamond operators to inferred types of denotable types. denotable types are Integer, Strings that can be applied generics to be used in java.

Java8 Diamond Operator anonymous Inner class example

Created an abstract Message class as follows

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

We will see how do create an instance by implementing the abstract class.
With java8, we can write code like the below for the above abstract implementation.

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

It is a valid code and the diamond operator is not used.

How do you write a code with 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);
   }
  };

The code is simple to readable. but this gives

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

Java9 allows using a diamond operator with anonymous for inner classes.

Java 9 Diamond Operator program example

Diamond operators can be used to inner classes too.

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 Anonyms Inner classes Example

Anonyms Inner class is used 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

since Lambda expressions are introduced in java8, Anonyms are not much used, But they improve the readability of the code and can be used if required.