Was it helpful?

Question

Reference to an instance method using method references in Java8

Java8Object Oriented ProgrammingProgramming

Lambda expressions In Java allows you to pass functionality as an argument to a method. You can also call an existing method using lambda expressions.

list.forEach(n -> System.out.println(n));

Method references are simple, easy-to-read lambda expressions to call/refer and existing method by name in a lambda expression.

Syntax

Following is the syntax to reference a instance method in Java

Object:methodName

Example

Following Java example references a instance method in Java.

interface myInterface{
   void greet();
}
public class MethodReferences {
   public static void demo() {
      System.out.println("Sample method");
   }
   public static void main(String args[]) {
      MethodReferences obj = new MethodReferences();
      myInterface in = obj::demo;
      in.greet();
   }
}

Output

Sample method
raja
Published on 08-Apr-2020 16:52:41
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top