Friday, August 24, 2018

Java9 Diamond operator anonymous inner class improvement with example

In this blog post, We are going to cover the Java9 features - Diamond Operator Improvement for allowing anonymous inner class with an example.

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
java9 Diamond Operator for Anonymous Inner class example
Left side is using Generics with a data type of String, Right side of the equal operator is raw type ArrayList and it inferred from left side type. We can apply diamond operators to inferred types of denotable types. DenotablJavapes are Integer, Strings that can be applied generics to be used in java.
For 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 we will create an instance by implementing the abstract class.

Java8 Diamond Operator anonymous Inner class example

With java8, we can write code like the below as for above abstract implementation
Message<String> stringMessage = new Message<String>("one") {
   @Override
   public void getMessage() {
    System.out.println(text);
   }
  };
  stringMessage.getMessage();
 
This is valid code and diamond operator is not used. How do you write a code with Diamond Operator. see Below
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 prior to it, Compiler throws 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);
   }
  };

Diamond Operator Anonyms Inner classes Example

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 is
one
two
 
since Lambda expressions are introduced in java8, Anonyms are not much use, But they improve the readability of the code and can be used if required.


Related article


EmoticonEmoticon