Method References Types in Java 8

There are four types of Method Reference in java 8, we will see each of them one by one.

Type Example
Reference to a static method       containingClass::staticMethodName
Reference to an instance method of a particular object       containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type      containingType::methodName
Reference to a constructor      ClassName::new

 

 

 

 

 

Example-1 : Reference to a static method

Output of the programme:

In our programme we mentioning 4 steps. All the four steps doing the same task. Lets see what each step is doing in above programme

Step1) In this step we are creating an EmployeeAgeComparator for Employee, which  we pass to the Arrys.sort method for sorting employee by their age.

Step 2) Notice that the interface Comparator is a functional interface. Therefore, we could use a lambda expression instead of defining and then creating a new instance of a class that implements Comparator.

Step3) However, this method to compare the date of brith two Employee instances already exists as Employee.compareByAge. You can invoke this method instead in the body of the lambda expression.

Step 4) Because this lambda expression invokes an existing method, you can use a method reference instead of a lambda expression. The method reference Employee::compareByAge is semantically the same as the lambda expression (emp1, emp2) -> Employee.compareByAge(emp1, emp2). The method reference Person::compareByAge is a reference to a static method.

 

Example-2: Reference to an instance method of a particular object

Output of the above programme:

In our programme we are mentioning 5 steps. Except Step -0, all further four steps we are just trying doing the same task with different code syntax. Lets see what each step is doing in above programme

Step 0) We are just simply creating an instance of class EmployeeComparisonProvider.

Step 1) In this step we are creating an EmployeeNameComparator for Employee, which  we pass to the Arrys.sort method for sorting employee by their name.

 Step 2) As we know interface Comparator is a functional interface. Therefore, we could use a lambda expression instead of defining and then creating a new instance of a class that implements Comparator.

Step3) However, this method to compare two Employee by there name  instances already exists as employeeComparisonProvider.compareByName, where employeeComparisonProvider is an instance of EmployeeComparisonProvider and compareByName is an instance method. You can invoke this method instead in the body of the lambda expression.

Step 4) Because this lambda expression invokes an existing method, you can use a method reference instead of a lambda expression.Its formal parameter list is copied from Comparator<Employee>.compare, which is (Employee, Employee). Its body calls the method employeeComparisonProvider.compareByName. The method reference employeeComparisonProvider::compareByName is a reference to a instance method.

 

Example-3 : Reference to an instance method of an arbitrary object of a particular type

 

Example-4 : Reference to a constructor

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *