Java > Core Java > Methods and Functions > Method References
Method Reference: Constructor Reference
This example showcases constructor references in Java. Constructor references are used to create new objects. It's a way to refer to a constructor without actually calling it, similar to how method references work with regular methods.
Core Concepts of Constructor References
Constructor references allow you to create new instances of a class without explicitly invoking the `new` keyword within a lambda expression. The syntax `ClassName::new` refers to the constructor of `ClassName`. This is particularly useful with functional interfaces that expect a function that returns an instance of a class.
Code Example: Creating Objects with Constructor References
This code defines a `Person` class with a constructor that takes a name. It then creates a list of names. The `map` operation uses `Person::new` as a constructor reference to create a new `Person` object for each name in the list. This is equivalent to `name -> new Person(name)`. The resulting `Person` objects are collected into a new list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{name='" + name + "'}";
}
}
public class ConstructorReferenceExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Using constructor reference to create a list of Person objects
List<Person> people = names.stream()
.map(Person::new)
.collect(Collectors.toList());
System.out.println("People: " + people);
}
}
Real-Life Use Case Section
Constructor references are valuable when dealing with factories or object creation scenarios. Imagine reading data from a file and creating objects based on each line. Using a constructor reference, you can cleanly map each line to a new object instance. This approach simplifies the creation process and enhances code maintainability. Another common case is in dependency injection frameworks.
Best Practices
Interview Tip
Be prepared to explain how constructor references work and when they are appropriate to use. Understand how they relate to functional interfaces and lambda expressions. A common question is to provide an example of how to use a constructor reference to create a list of objects from a list of data.
When to Use Them
Use constructor references when you need to create instances of a class as part of a functional operation, such as mapping a list of strings to a list of objects. This is particularly useful when the object creation logic is simple and directly corresponds to the constructor's parameters.
Alternatives
The main alternative to constructor references is lambda expressions with explicit `new` calls. For example, `name -> new Person(name)` is an alternative to `Person::new`. Lambda expressions can be useful when you need to perform additional initialization or customization after the object is created.
Pros
Cons
FAQ
-
Can I use constructor references with constructors that have multiple parameters?
Yes, you can use constructor references with constructors that have multiple parameters, as long as the functional interface's abstract method signature matches the constructor's parameter types. -
Are constructor references only useful with streams?
No, constructor references can be used in any context where a functional interface is expected and the constructor's signature matches the functional interface's abstract method. They are frequently used with streams but are not limited to them.