What is Method Reference?
Method reference is a shorter way to call existing methods using a reference to them, without
writing full lambda expressions.
It's introduced in Java 8 as a feature of Functional Programming.
What is a Method Reference?
It is a compact way to refer to a method without executing it. The syntax is:
ClassName::methodName
Why Do We Need Method Reference?
Sometimes, in lambda expressions, you are just calling a method.
Writing () -> [Link]() feels a bit too long.
Method reference gives you a shortcut.
How Method Reference Works (Simple Example):
// Functional Interface
@FunctionalInterface
interface Engine {
void start();
}
public class Car {
public void startEngine() {
[Link]("Engine started using method reference!");
}
public static void main(String[] args) {
Car car = new Car();
// Method Reference
Engine engine = car::startEngine;
// This will internally call startEngine()
[Link]();
}
}
Step-by-Step Breakdown:
Step Explanation
1. Define a functional interface Engine with method start().
2. Create a class Car with method startEngine().
3. In main, create an object of Car: Car car = new Car();
4. Use method reference: Engine engine = car::startEngine;
- This means: "When [Link]() is called, it will call [Link]()."
5. Call: [Link]();
6. The message prints.
Method Reference Syntax:
Type Syntax Example Description
Static Method ClassName::staticMethod Math::abs Calls a static method
Reference to an Calls an instance method
instance method using a specific object
object::instanceMethod car::startEngine
of a particular
object
Reference to an Applies to any object of that
instance method type
of an arbitrary ClassName::instanceMethod String::toLowerCase
object of a
particular type
Constructor Refers to a class constructor
ClassName::new Car::new
Reference
Final Simple Example Again:
// Lambda way
Engine e1 = () -> [Link]();
// Method reference way (shorter)
Engine e2 = car::startEngine;
1. Reference to a Static Method
@FunctionalInterface
interface Calculator {
int operation(int a, int b);
}
public class StaticMethodReference {
public static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
// Method reference to static method
Calculator calc = StaticMethodReference::add;
int result = [Link](10, 20);
[Link]("Addition Result: " + result);
}
}
2. Reference to an Instance Method
@FunctionalInterface
interface Printer {
void print();
}
public class InstanceMethodReference {
public void showMessage() {
[Link]("Hello from instance method reference!");
}
public static void main(String[] args) {
InstanceMethodReference obj = new InstanceMethodReference();
// Method reference to instance method of object 'obj'
Printer printer = obj::showMessage;
[Link]();
}
}
3. Instance Method Reference (Class) Example:
import [Link];
public class ClassInstanceMethodExample {
public static void main(String[] args) {
Function<String, String> func = String::toUpperCase;
[Link]([Link]("hello"));
}
}
Output: HELLO
4. Reference to a Constructor
import [Link];
class Car {
Car() {
[Link]("Car is created");
}
}
public class ConstructorRef {
public static void main(String[] args) {
// Using lambda
Supplier<Car> lambdaCar = () -> new Car();
// Using constructor reference
Supplier<Car> refCar = Car::new;
[Link]();
[Link]();
}
}
Constructor Reference Example:
@FunctionalInterface
interface Message {
InstanceConstructorExample getMessage(String msg);
}
public class InstanceConstructorExample {
public InstanceConstructorExample(String msg) {
[Link](msg);
}
public static void main(String[] args) {
Message message = InstanceConstructorExample::new;
[Link]("Hello from Constructor Reference!");
}
}
Output: Hello from Constructor Reference!
✅ Benefits of Method Reference:
Simple and clean syntax.
Reduces boilerplate code.
Improves readability.
Used with Functional Interfaces (e.g., Runnable, Consumer, Supplier).
Good for Streams, Collections, Lambda expressions replacement.
✅ Quick Example Comparing Lambda vs Method
Reference:
[Link](name -> [Link](name)); // Lambda
[Link]([Link]::println); // Method Reference (Cleaner)
✅ Summary:
Method Reference = Shortcut for Lambda expressions when just calling an existing method.
Helps to write simple, readable, and functional style Java code.
More Example 1: Static Method Reference - Multiplication
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class MultiplyExample {
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
Calculator calc = MultiplyExample::multiply;
[Link]("Product: " + [Link](5, 4));
}
}
✅ Output:
Product: 20
Example 2: Static Method Reference - Find Maximum
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class MaxExample {
public static int max(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
Calculator calc = MaxExample::max;
[Link]("Maximum: " + [Link](10, 25));
}
}
✅ Output:
Maximum: 25
Example 3: Static Method Reference - Subtraction
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class SubtractExample {
public static int subtract(int a, int b) {
return a - b;
}
public static void main(String[] args) {
Calculator calc = SubtractExample::subtract;
[Link]("Difference: " + [Link](50, 30));
}
}
✅ Output:
Difference: 20
Example 4: Static Method Reference - GCD (Greatest
Common Divisor)
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class GcdExample {
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
Calculator calc = GcdExample::gcd;
[Link]("GCD: " + [Link](36, 60));
}
}
✅ Output:
GCD: 12
✅ Summary:
All these examples follow the same pattern:
1. Define a functional interface (Calculator).
2. Define a static method (add, multiply, subtract, gcd).
3. Refer to that method using method reference: ClassName::method.
4. Call through interface method.
Logical Difference:
Program 1 Program 2
You are defining behavior (calculate()) in a You are reusing an existing static method (add()) as
concrete class (MethodExample). implementation of the interface method.
Traditional OOP approach (class + interface Functional programming style using method
implementation). reference.
Requires explicit object creation (new No object creation needed (uses static method
MethodExample()). directly).
Technical Difference:
OOP way Method Reference way
Interface method is implemented in a class No need to implement interface manually; method
(implements Calculator). reference links the static method to the interface method.
Explicit object instantiation is needed. No object creation; static method reference used directly.
Verbose (more code, more boilerplate). Cleaner, shorter syntax with method references.
Cannot be used with Streams, functional Works well with Streams, Lambdas, functional APIs.
OOP way Method Reference way
APIs.
Good for reusable, complex
Good for simple utility method reuse.
implementations.
In simple words:
Program 1 Program 2
Old school way of coding (traditional OOP). Modern, functional programming style (Java 8+).
You manually write a class to implement behavior. You just refer to an existing method and assign it.