Skip to content

Reference to a Constructor (Method References)

In this lesson, you will learn about the fourth and final kind of method reference, "a reference to a constructor".

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

Reference to a Constructor

Constructor references are specialized forms of method references that refer to the constructors of a class. They can be created using the className and the keyword new.

Syntax

ClassName::new

Example

Let us write a simple example that contains four files.

Let's assume we have a class called Person that takes a String argument in its constructor to set the name of the person. We can create a method reference to this constructor using the following syntax:

Person::new

This creates a reference to the constructor of the Person class that takes a String argument. We can then use this constructor reference to create new Person objects by passing the String argument to the constructor reference.

For example, let's say we have a list of names and want to create a list of Person objects with those names. We can use the map method of the Stream class along with the Person::new constructor reference like this:

public class Person {
  private final String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  @Override
  public String toString() {
    return name;
  }
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ReferenceToConstructor {
  public static void main(String[] args) {
    List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

    List<Person> people =
        names // collection of names stored in list
            .stream() // stream of names
            .map(Person::new) // calling the constructor
            .collect(Collectors.toList());

    System.out.println(people);
  }
}

In this example, we use the map method to apply the Person::new constructor reference to each name in the names list. We pass the name as an argument to the constructor reference to create a new Person object with that name. Finally, we collect the Person objects into a new list using the toList or .collect(Collectors.toList()) method. The output of this program will be:

[Alice, Bob, Charlie]
Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.