Learnitweb

Method references in Java

Method references are a special type of lambda expressions. This feature was introduced in Java 8. Method references are often used to create simple lambda expressions by referencing existing methods.

We use lambda expressions to create anonymous methods. Sometimes, a lambda expression does nothing but calls an existing method. In such cases, method references can be used by referring to existing methods by name.

There are four kinds of method references.

  • Reference to a static method
  • Reference to an instance method of a particular object
  • Reference to an instance method of an arbitrary object of a particular type
  • Reference to a constructor

Reference to a static method

Syntax: ContainingClass::staticMethodName

interface Printable{  
    void print();  
}  
public class MethodReferenceExample {  
    
	public static void printSomething(){  
        System.out.println("static method called.");  
    }  
    public static void main(String[] args) {  
        // Referring static method  
        Printable printable = MethodReferenceExample::printSomething;  
        // Calling interface method  
        printable.print();  
    }  
} 

Output

static method called.

Without method reference, we can write the lamba expression as

Printable printable = () -> {
        	printSomething();
        };

Reference to an instance method of a particular object

Syntax: containingObject::instanceMethodName

To refer a non-static method of a class, we have to use the reference of that class. In the following example, we have shown two ways to access a non-static method of a class. First one is by creating an object and referring the method printSomething() method using the object. In the second, we are referring method using anonymous object.

interface Printable{  
    void print();  
}  
public class MethodReferenceExample {  
    
	public void printSomething(){  
        System.out.println("instance method called.");  
    }  
    public static void main(String[] args) {  
        MethodReferenceExample methodReference = new MethodReferenceExample();
        
        //referring non-static method using reference
        Printable firstPrintable = methodReference::printSomething;
        
        firstPrintable.print();
        
        //Referring non-static method using anonymous object
        Printable secondPrintable = new MethodReferenceExample()::printSomething;
        
        firstPrintable.print();
    }  
}  

Output

instance method called.
instance method called.

Reference to an Instance Method of an Arbitrary Object of a Particular Type

This type of method reference is similar to the previous example. The only difference is we do not have to create a custom object.

import java.util.Arrays;

public class MethodReferenceExample {  
      
    public static void main(String[] args) {  
    	String[] stringArray = { "Sun", "Mon", "Tue", "Wed",
    		    "Thu", "Fri", "Sat"};
    		Arrays.sort(stringArray, String::compareToIgnoreCase);
    		
    	for(String str: stringArray) {
    		System.out.println(str);
    	}
    }  
}

Output

Fri
Mon
Sat
Sun
Thu
Tue
Wed

Reference to a constructor

You can reference a constructor in the same way as a static method by using the new. In the following example, we are referencing the constructor of Sport class.

import java.util.Arrays;

interface Playable{
	Sport getSport(String sport);
}

class Sport {
	Sport(String sport){
		System.out.println("Sport constructor called with argument " + sport);
	}
}

public class MethodReferenceExample {  
      
    public static void main(String[] args) {  
    	Playable playable = Sport::new;
    	playable.getSport("cricket");
    }  
}

Output

Sport constructor called with argument cricket

The lambda expression in above example is shorthand of the following code:

Playable playable = (sport) -> {
return new Sport(sport);
};