Java 8 : Method References

By | April 12, 2014

 This article contains…

  • Method References
  • Example of method reference
  • When to use method reference
  • When you can not use method reference
  • Kind of method references

In previous article about Lambda Expression, we explained how to use anonymous methods or Lambda expressions. Before moving forward, It is recommended to take a look at Lambda Expressions in java if you are new to Java 8.

Method References :

Some times, A lambda expression do nothing but just calls a method which is already defined.
In this case, to be more lazy to write, You can use method references.

Method references are just compact and more readable form of a lambda expression for already written methods. “::” operator is used for method reference.

Example :

// Don't forget Functional interfaces are heart of labmda expression.
interface IsReferable {
   public void referenceDemo();

class ReferenceDemo {

   public static void commonMethod() {
      System.out.println("This method is already defined.");

   public void implement() {

      // Anonymous class.
      IsReferable demoOne = new IsReferable() {
         public void referenceDemo() {

      // Lambda implementaion.
      IsReferable demo = () -> ReferenceDemo.commonMethod();

      // Method reference.
      IsReferable demoTwo = ReferenceDemo::commonMethod;

In above example, all three implementations, 1) using anonymous class 2) Lambda Expression and 3)Method reference will give same results. Anonymous class implementation is added to provide readability.

So a method reference will call the method mentioned right side of “::” from the body of implemented only abstract method of Functional interface.

When to use method reference :

When a Lambda expression is invoking already defined method, you can replace it with reference to that method.

When you can not use Method reference :

You can not pass arguments to the method reference.
for example, you can not use method reference for following lambda.

IsReferable demo = () -> ReferenceDemo.commonMethod("Argument in method.");

Because Java does not support currying without Wrapper methods or Lambda.[1]

Kind of method references :

As mentioned in the Javadoc, there are four kind of Method references.

  1. Reference to a static method : ClassName::staticMethod
    Example :


    Where ReferenceDemo is class in which static method commonMethod defined.

  2. Reference to an instance method of a particular object : ObjectOfClass::instanceMethod
    Example :


    Where demo is and object reference for ReferenceDemo class and anotherMethod is instance method defined in class.

  3. Reference to an instance method of an arbitrary object of a particular type : ContainingType::methodName
    Example :


    Method for and perticular arbitary object type like String, Integer etc…

  4. Reference to a constructor : ClassName::new
    Example :


    which is reference to the String class constructor.

So, Method reference is smaller feature of java 8 compare to gigantic Lambda and other cool features.
Useful Links :

Query posted on stackoverflow by me before writing this article.

You can always clone the executable code of article posted on Java By Examples from
Repository URL :

Don’t forget to write your reviews and feedback. You can post as comments or write us on

3 thoughts on “Java 8 : Method References

Leave a Reply

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