What is Method Reference in Java 8?

In this article, we will understand what is a method reference in java 8 with proper examples.

Method reference means referring to an existing definition instead of duplicating the same definition again. Method references are shorthand notations for lambda expressions. It means we don’t have to provide the definition again if the definition already exists.

Benefits of method reference

  • Reduce the codebase
  • Adds up simplicity
  • Increases the readability

Syntax of method reference

Class-Name :: method-name

If method is static

Instance :: method-name

If method is instance

Ex : Employee :: getSalary
employee :: getSalary

Instance method reference

We will refer to instance methods using the class instance name. We will consider a lambda expression as below :

@FunctionalInterface
interface Addition {
	int add(int a, int b);
}

public class LambdaExp {
	public static void main(String[] args) {
		Addition addition = (int a, int b) -> { return a+b ; } ;
		int result = addition.add(5, 10);
		System.out.println("Addition is : "+result);
	}
}

O/P : Addition is : 15

In the above example, we have implemented the “add” method as a lambda expression.

Now consider a case when the definition of an add method is already available to us, then is it necessary to provide a lambda definition again? No, it is not necessary we can make use of the method which already exists. So we will be referring to the existing method instead of creating a new lambda expression again. Please check the example below :

@FunctionalInterface
interface Addition {
	int add(int a, int b);
}

public class InstanceMethodReference {
	int sum(int a, int b) {
		return a+b;
	}
	
	public static void main(String[] args) {
		InstanceMethodReference instanceMethodReference = new InstanceMethodReference();
		Addition addition = instanceMethodReference::sum;
		int result = addition.add(5, 10);
		System.out.println("Addition is : "+result);
	}
}

O/P : Addition is : 15

In the above example, we already have a method definition for adding two numbers in the “sum” method. So we will be referring to the existing definition instead of creating a new one . “add” method is referred to “sum” method, here we did not implement add method instead we referred “sum” method.

As “sum” method is a instance method so we have to create a class instance (instanceMethodReference) first and then make use of “sum” via “instanceMethodReference”. The above scenario is called “Instance Method Reference”.

Static Method Reference :

We will refer to the static method using the class name . Refer to the example below :

@FunctionalInterface
interface Addition {
	int add(int a, int b);
}

public class StaticMethodReference {
	static int sum(int a, int b) {
		return a+b;
	}
	
   public static void main(String[] args) {
		Addition addition = StaticMethodReference::sum;
		int result = addition.add(5, 10);
		System.out.println("Addition is : "+result);
	}
}

O/P : Addition is : 15

Points to Remember

Different return types are allowed

In the case of method reference, different return types are allowed, but argument types must be matched.

Example-1: Using float return type in FunctionalInterface, but int return type which we are going to refer

@FunctionalInterface
interface Addition {
	float add(int a, int b);
}

public class RememberPoint1 {
	int sum(int a, int b) {
		return a+b;
	}
	
    public static void main(String[] args) {
	   	RememberPoint1 rememberPoint1 = new RememberPoint1();
		Addition addition = rememberPoint1::sum;
		int result = (int) addition.add(5, 10);
		System.out.println("Addition is : "+result);
	}
}


O/P : Addition is : 15

Example-2: Using void return type in FunctionalInterface, but int return type which we are going to refer

@FunctionalInterface
interface Addition {
	void add(int a, int b);
}

public class RememberPoint1 {
	int sum(int a, int b) {
		int result = a+b;
		System.out.println("Addition is : "+result);
		return result;
	}
	
    public static void main(String[] args) {
	    RememberPoint1 rememberPoint1 = new RememberPoint1();
		Addition addition = rememberPoint1::sum;
		addition.add(5, 10);
	}
}

O/P : Addition is : 15

Example-3: Compilation exception when the argument mismatches

@FunctionalInterface
interface Addition {
	float add(int a, int b);
}

public class RememberPoint1 {
	int sum(int a, int b, int c) {
		return a+b;
	}
	
    public static void main(String[] args) {
	    RememberPoint1 rememberPoint1 = new RememberPoint1();
		Addition addition = rememberPoint1::sum; 
		int result = (int) addition.add(5, 10);
		System.out.println("Addition is : "+result);
	}
}

CE : The type RememberPoint1 does not define sum(int, int) that is applicable here

Different access modifiers are allowed

Different access modifiers are allowed, but argument types must be matched. In the example below we are using the public modifier for the “add” method in the functional interface whereas the private modifier for the “sum” method.

@FunctionalInterface
interface Addition {
	public int add(int a, int b);
}

public class RememberPoint2 {
	private int sum(int a, int b) {
		return a+b;
	}
	
    public static void main(String[] args) {
	    RememberPoint2 rememberPoint2 = new RememberPoint2();
		Addition addition = rememberPoint2::sum;
		int result = (int) addition.add(5, 10);
		System.out.println("Addition is : "+result);
	}
}

O/P : Addition is : 15

In this article, we have covered what is a method reference in Java 8 along with proper examples. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send an email.

Leave a Comment