Method References and Constructor References are features introduced in Java 8 that make lambda expressions even more concise. They allow you to refer to methods or constructors without invoking them, offering a clean and readable syntax for functional programming.
1. Method References
A method reference is a shorthand notation of a lambda expression to call a method. It uses the ::
operator.
Types of Method References:
Type | Syntax | Example |
---|---|---|
Reference to a static method | ClassName::staticMethodName |
Math::abs |
Reference to an instance method of a particular object | object::instanceMethodName |
System.out::println |
Reference to an instance method of an arbitrary object of a particular type | ClassName::instanceMethodName |
String::toLowerCase |
Example 1: Static Method Reference
import java.util.function.BiFunction; public class StaticMethodRef { public static int multiply(int a, int b) { return a * b; } public static void main(String[] args) { BiFunction<Integer, Integer, Integer> func = StaticMethodRef::multiply; System.out.println("Result: " + func.apply(3, 4)); // Output: Result: 12 } }
Example 2: Instance Method Reference (Specific Object)
import java.util.function.Consumer; public class InstanceMethodRef { public void show(String message) { System.out.println("Message: " + message); } public static void main(String[] args) { InstanceMethodRef obj = new InstanceMethodRef(); Consumer<String> printer = obj::show; printer.accept("Hello from instance method!"); } }
Example 3: Instance Method Reference (Arbitrary Object)
import java.util.Arrays; import java.util.List; public class ArbitraryObjectMethodRef { public static void main(String[] args) { List<String> names = Arrays.asList("Java", "Python", "C++", "Ruby"); names.forEach(String::toUpperCase); // No output since toUpperCase returns but doesn't print // To see the result names.stream().map(String::toUpperCase).forEach(System.out::println); } }
2. Constructor References
Constructor references let you refer to a constructor without instantiating it directly. They are often used with functional interfaces like Supplier
, Function
, etc.
Syntax
ClassName::new
Code language: JavaScript (javascript)
Example: Constructor Reference with Supplier
import java.util.function.Supplier; class Product { public Product() { System.out.println("Product Created!"); } } public class ConstructorRefExample { public static void main(String[] args) { Supplier<Product> supplier = Product::new; Product p = supplier.get(); // Output: Product Created! } }
Example: Constructor Reference with Parameters using Function
import java.util.function.Function; class Employee { String name; public Employee(String name) { this.name = name; System.out.println("Employee Created: " + name); } } public class ConstructorWithArg { public static void main(String[] args) { Function<String, Employee> employeeCreator = Employee::new; employeeCreator.apply("Mahesh"); } }
Final thought on Method References and Constructor References
Feature | Example | Description |
---|---|---|
Static Method Reference | ClassName::staticMethod |
Calls static methods directly |
Instance Method (specific object) | obj::method |
Calls method on specific object |
Instance Method (arbitrary object) | Class::method |
Calls method on any object of class |
Constructor Reference | Class::new |
Creates new instance |
When to Use:
-
When lambda expressions are used just to call an existing method, method references make code shorter and clearer.
-
Constructor references are useful when you need to supply or create objects dynamically, e.g., in streams, factories, etc.
Method references and constructor references are powerful enhancements introduced in Java 8 that simplify the use of lambda expressions. By allowing developers to refer to existing methods or constructors using the ::
operator, these features help improve code readability, conciseness, and reusability.
They are especially useful in functional programming scenarios where operations like filtering, mapping, creating objects, or printing values are common, such as with streams and collections. Whether referencing static methods, instance methods, or constructors, this syntax eliminates boilerplate and encourages a cleaner, more expressive coding style.
Mastering method and constructor references will not only make your Java code more elegant but also enhance your ability to write modern, functional, and efficient Java applications.