Java Method Reference ::

Last modified on August 1st, 2014 by Joe.

Method reference in Java 8 is the ability to use a method as an argument for a matching functional interface. :: (double colon) is the operator used for method reference in Java. An interface with only one method is called a functional interface. For example, Comparable, Runnable, AutoCloseable are some functional interfaces in Java.

Its there all through the Internet saying Java does not have the scope resolution operator (::), in comparison with C++. Its on the way and it will be here with Java 8. Between Java and C++, only the operator :: (double colon) is same, the way it is implemented is different. So we cannot call it as scope resolution operator in Java. It is used for method referencing and not for scope resolution.

Method-Reference

Method References and Lambda Expressions

Method reference using :: is a convenience operator. Method reference is one of the features belonging to Java lambda expressions. Method reference can be expressed using the usual lambda expression syntax format using –> In order to make it more simple :: operator can be used.

In the last tutorial, I gave a list of examples for Java lambda expressions. Which will give detail on different ways of using lambda expressions and I recommend you to go through it using JDK 8. In that article refer example 5 (Lambda Expression Sorting), there we can see method reference used.

Syntax: <classname or instancename>::<methodname>

The method in the functional interface and the passing method reference should match for the argument and returntype. Method reference can be done for both static and class methods. Let us consider the following example to understand this. In AutoCloseable interface we have the method of signature as,

void close throws Exception()

In our example below we have the method with signature,

void close()

So these two methods are matching in terms of argument and returntype (type match). Therefore we can use the method we have written for the functional interface AutoCloseable as a method reference.

Method Reference Example

package com.javapapers.java8;

public class MethodReferenceExample {
	void close() {
		System.out.println("Close.");
	}

	public static void main(String[] args) throws Exception {
		MethodReferenceExample referenceObj = new MethodReferenceExample();
		try (AutoCloseable ac = referenceObj::close) {
		}
	}
}

Constructor References

Constructor reference is similar to method references, wherein used at the place of constructors. It gives best opportunity to use at factory scenarios. in the following example, we are using the zoo constructor with argument as constructor reference. I have provided the equivalent line in lambda expression in the code, just to understand it.

Constructor Reference Example

package com.javapapers.java8;

import java.util.List;
import java.util.ArrayList;

class Zoo {
    private List animalList;
    public Zoo(List animalList) {
        this.animalList = animalList;
		System.out.println("Zoo created.");
    }
}

interface ZooFactory {
    Zoo getZoo(List animals);
}
 
public class ConstructorReferenceExample {

    public static void main(String[] args) {
		//following commented line is lambda expression equivalent
		//ZooFactory zooFactory = (List animalList)-> {return new Zoo(animalList);};	
		ZooFactory zooFactory = Zoo::new;
		System.out.println("Ok");		
		Zoo zoo = zooFactory.getZoo(new ArrayList());
    }
}

Comments on "Java Method Reference ::"

  1. santosh says:

    Hi Joe,

    Thanks again for another nice blog. I feel myself up-to-date when I go through your such posts.

    I have one request/suggestion. Could you kindly post the output also along with every code examples you put in the blogs? This will really be helpful to understand the immediate output of a given example.

    Thanks,
    Santosh

  2. Pratik says:

    started loving Java8 new features.
    Thanks for your article JOE

  3. Pattabi Raman says:

    Kindly update the codes with output and source code files which will be very useful for us. Your posts are awesome!!… Thanks…

  4. Rajeesh says:

    Im not able to catch up the exact use of this feature :(

  5. Myself says:

    I got it clear after I went through the topics on lamdba Expression and Examples.. Thanks Joe..

Comments are closed for "Java Method Reference ::".