Method Reference In Java 8.

One of the most welcome changes in Java 8 was the introduction of Lambda Expression and Method Reference In java 8..

Method references in java are a special type of lambda expressions. because they’re often used to create simple lambda expressions by referencing existing methods.

It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.

So in this , we are explaining method reference in java concept in detail.

Types of method reference in java.

  1. Method Reference to a static method.
  2. Reference to a instance method.
  3. Reference to a constructor.

1. Method Reference to a static method.

syntax :-

ContainingClass::staticMethodName

Example:-

In the below example, we have defined a functional interface and referring a static method to it’s functional method max().

interface Maxester{  
    void max();  
}  
public class MethodReference {  
    public static void welcome(){  
        System.out.println("Welcome, this is static method.");  
    }  
    public static void main(String[] args) {  
        // Referring static method  
        Maxester maxester = MethodReference::welcome;  
        // Calling interface method  
        maxester.max();  
    }  
}  

Output:-

Welcome, this is static method.

2. Reference to a instance method.

syntax:-

 containingObject::instanceMethodName 

Example:-

In the below example, we are referring non-static methods. we can refer methods by class object and also an anonymous object.

interface Maxester{  
    void max();  
}  
public class InstanceMethodReference {  
    public void welcome(){  
        System.out.println("Welcome, this is instance method.");  
    }  
    public static void main(String[] args) {  
        InstanceMethodReference methodReference = new InstanceMethodReference(); // Creating object  
        // Referring non-static method using reference  
            Maxester maxester = methodReference::welcome;  
        // Calling interface method  
            Maxester.max();  
            // Referring non-static method using anonymous object  
            Maxester maxester_first = new InstanceMethodReference()::welcome; // You can use anonymous object also  
            // Calling interface method  
            maxester_first.max();  
    }  
}  

Output:-

Welcome, this is instance method.

Welcome, this is instance method.

3. Reference to a constructor.

syntax:-

 ClassName::new

Example:-

Interface Maxester{
      NewMaxester max(String value);
}
class NewMaxester{
      NewMaxester(String value){
         System.out.prinln(value);
     }
}
public class ConstrutorRefrenceMethod{
      public static void main(String[] args){
             Maxester maxester = NewMaxester::new;
             maxester.max("Welcome to maxester constructor");
        }
}

output:-

Welcome to maxester constructor.

External Link:-

You can also read much more about Method Reference from below link.

https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

So i hope, This will be helpful for you.

ThankYou.

Leave a Reply

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