🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Method References  


Introduction
  • Method References were introduced in Java 8.
  • A Method Reference is a shorthand way to call a method using :: (double colon operator).
  • It is an alternative to writing a lambda expression when the lambda only calls an existing method.
  • Use of Method Reference:
    • Simplifies Lambda Expressions:
      • If lambda just calls a method, we can replace it with method reference.
    • Improves Readability:
      • Code looks clean and direct.
    • Avoids Boilerplate:
      • No need to rewrite parameter passing manually.
  • Types of Method Reference:
    • There are four types of method references that are as follows:
      1. Reference to an Instance Method
      2. Reference to an Instance Method of a Particular Type
      3. Reference to a Static Method
      4. Reference to a Constructor
    • These all are explained below !

1. Reference to an Instance Method:
  • This type lets us use a method from an object we already have. Instead of writing a new function, we can directly refer to that object’s method.
  • Syntax:
    • object::instanceMethodName
  • Program:
    @FunctionalInterface
    interface MyInterface
    {
        void greet();
    }
    
    class Greeting
    {
        void sayHello()
        {
            System.out.println("Hello from instance method!");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Greeting g = new Greeting();
    
            // Way 1 : Using Lambda
            MyInterface obj1 = () -> g.sayHello();
            obj1.greet();
    
            // Way 2 : Using Method Reference
            MyInterface obj2 = g::sayHello;
            obj2.greet();
        }
    }

2. Reference to an Instance Method of a Particular Type
  • It means calling a method on any object of a class (not just one specific object). It is useful when we want to perform the same action on many objects, and it helps us write less code.
  • Syntax:
    • ClassName::instanceMethodName
  • Program:
    @FunctionalInterface
    interface MyInterface
    {
        void displayMsg(String msg);
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            // Way 1 : Using Lambda
            MyInterface obj1 = (msg) -> System.out.println(msg);
            obj1.displayMsg("Hello World!");
    
            // Way 2 : Using Method Reference
            MyInterface obj2 = System.out::println;
            obj2.displayMsg("Hello World!");
        }
    }

3. Reference to a Static Method
  • A static method reference lets us call a class’s static method directly, without writing extra code. It’s a shorter way to replace a lambda that only calls that method.
  • Syntax:
    • ClassName::staticMethodName
  • Program:
      @FunctionalInterface
      interface MyInterface
      {
          void display();
      }
      
      class Demo
      {
          static void showMessage()
          {
              System.out.println("Hello from static method!");
          }
      }
      
      public class MainApp
      {
          public static void main(String[] args)
          {
              // Way 1 : Using Lambda
              MyInterface obj1 = () -> Demo.showMessage();
              obj1.display();
      
              // Way 2 : Using Method Reference
              MyInterface obj2 = Demo::showMessage;
              obj2.display();
          }
      }

4. Reference to a Constructor
  • A constructor reference lets us create a new object quickly without extra code. It’s a shortcut for calling a class’s new method.
  • Syntax:
    • ClassName::new
  • Program:
    @FunctionalInterface
    interface MyInterface
    {
        Greeting getGreeting();
    }
    
    class Greeting
    {
        Greeting()
        {
            System.out.println("Greeting object created!");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            // Way 1 : Using Lambda
            MyInterface obj1 = () -> new Greeting();
            obj1.getGreeting();
    
            // Way 2 : Using Constructor Reference
            MyInterface obj2 = Greeting::new;
            obj2.getGreeting();
        }
    }