Method Reference Operator

Method references using (::) Operator
  • Functional Interface method can be mapped to our specified method by using :: (double colon) operator. This is called method reference.
  • Our specified method can be either static method or instance method.
  • Functional Interface method and our specified method should have same argument types, except this the remaining things like return type, method name, modifiers etc are not required to match.

Syntax

If our specified method is static method

Classname::methodName

If the method is instance method

Objreference::methodName

Functional Interface can refer lambda expression and Functional Interface can also refer method reference. Hence lambda expression can be replaced with method reference. Hence method reference is alternative syntax to lambda expression.

E.g

With Lambda Expression

package com.ashok.java8.lamba;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class LambdaTest {
   public static void main(String[] args) {
      Runnable r = () -> {
         for (int i = 0; i < 10; i++) {
            System.out.println("Child Thread");
         }
      };
      
      Thread t = new Thread(r);
      t.start();
      
      for (int i = 0; i < 10; i++) {
         System.out.println("Main Thread");
      }
   }
}

With Method Reference

package com.ashok.java8.methodreference;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MethodReferenceTest {
   public static void main(String[] args) {
      Runnable r = MethodReferenceTest::m1;
      
      Thread t = new Thread(r);
      t.start();
      
      for (int i = 0; i < 10; i++) {
         System.out.println("Main Thread");
      }
   }

   private static void m1() {
      for (int i = 0; i < 10; i++) {
         System.out.println("Child Thread");
      }
   }
}

In the above example Runnable interface run() method referring to MethodReferenceTest class static method m1().

Method reference to Instance method

package com.ashok.java8.methodreference;

/**
 * 
 * @author ashok.mariyala
 *
 */
interface Interf {
 public void m1(int i);
}

class Test {
   public void m2(int i) {
      System.out.println("From Method Reference : " + i);
   }

   public static void main(String[] args) {
      Interf f = i -> System.out.println("From Lambda Expression : " + i);
      f.m1(100);
      Test t = new Test();
      Interf i1 = t::m2;
      i1.m1(200);
   }
}

In the above example functional interface method m1() referring to Test class instance method m2(). The main advantage of method reference is we can use already existing code to implement functional interfaces (code reusability).

Constructor References

We can use :: (double colon )operator to refer constructors also

Syntax

classname :: new

E.g

Interf f = sample :: new;

Here functional interface f referring sample class constructor.

E.g

package com.ashok.java8.constructorreference;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ConstructorReferenceTest {
   public static void main(String[] args) {
      Interf f = s -> new Sample(s);
      f.get("From Lambda Expression");
      Interf f1 = Sample::new;
      f1.get("From Constructor Reference");
   }
}

interface Interf {
   public Sample get(String s);
}

class Sample {
   private String str;

   public Sample(String str) {
      this.str = str;
      System.out.println("Constructor Executed : " + this.str);
   }
}

Output

Constructor Executed : From Lambda Expression
Constructor Executed : From Constructor Reference

Note 

In method and constructor references compulsory the argument types must be matched.

Method Reference Operator


Scroll to top