Implement Constructor Reference with One or More Arguments in Java



In this article, we will learn to implement a constructor reference with one or more arguments in Java. Constructor references in Java give us a shorter method of instantiating new objects through method references. These will help us reference a constructor without actually calling it thus the code is cleaner and it will be more readable.

What is a Constructor Reference?

A method reference can also be applicable to constructors in Java 8. A constructor reference can be created using the class name and a new keyword. The constructor reference can be assigned to any functional interface reference that defines a method compatible with the constructor.

Syntax

<Class-Name>::new

Constructor Reference with One Argument

To make it clearer we will now consider a scenario where we must create Student objects using a constructor that accepts a single argument (name).

Following are the steps to implement a constructor reference with one argument ?

  • mf = Student::new:Constructor reference that assigns the Student(String name) constructor to mf.
  • Function<String, Student> f1 = Student::new: Here it uses Java's Function functional interface to create a Student object.
  • Function<String, Student> f2 = (name) -> new Student(name): This expression is equivalent lambda expression.

Example

Below is an example of implementing a constructor reference with one argument ?

import java.util.function.*;

@FunctionalInterface
interface MyFunctionalInterface {
   Student getStudent(String name);
}
public class ConstructorReferenceTest1 {
   public static void main(String[] args) {
      MyFunctionalInterface mf = Student::new;

      Function<String, Student> f1 = Student::new;    // Constructor Reference
      Function<String, Student> f2 = (name) -> new Student(name);

      System.out.println(mf.getStudent("Adithya").getName());
      System.out.println(f1.apply("Jai").getName());
      System.out.println(f2.apply("Jai").getName());
   }
}

// Student class
class Student {
   private String name;
   public Student(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

Output

Adithya
Jai
Jai

Time Complexity: O(1), direct object creation.
Space Complexity: O(1), only one object is created.

Constructor Reference with Two Arguments

When a constructor requires multiple arguments, we can use functional interfaces like BiFunction<T, U, R>.

Following are the steps to implement a constructor reference with two arguments ?

  • mf = Student::new: Constructor reference for the Student(int id, String name) constructor.
  • BiFunction<Integer, String, Student> f1 = Student::new: Here it uses BiFunction to refer to the constructor.
  • BiFunction<Integer, String, Student> f2 = (id, name) -> new Student(id, name): The given expression is equivalent to lambda expression.

Example

Below is an example of implementing a constructor reference with two arguments ?

import java.util.function.*;

@FunctionalInterface
interface MyFunctionalInterface {
   Student getStudent(int id, String name);
}
public class ConstructorReferenceTest2 {
   public static void main(String[] args) {
      MyFunctionalInterface mf = Student::new;    //  Constructor Reference

      BiFunction<Integer, String, Student> f1 = Student::new;
      BiFunction<Integer, String, Student> f2 = (id, name) -> new Student(id,name);

      System.out.println(mf.getStudent(101, "Adithya").getId());
      System.out.println(f1.apply(111, "Jai").getId());
      System.out.println(f2.apply(121, "Jai").getId());
   }
}

// Student class
class Student {
   private int id;
   private String name;
   public Student(int id, String name) {
      this.id = id;
      this.name = name;
   }
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

Output

101
111
121

Time Complexity: O(1), direct object creation with two parameters.
Space Complexity: O(1), only one object is created.

Conclusion

Constructor references in Java are a new and simple means to initialize objects using method references. They reduce object creation to simple and readable code, facilitating maintenance. Functional interfaces such as Function<T, R> and BiFunction<T, U, R>, by using these we can map one-parameter and more-than-one-parameter constructors easily.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-03-21T19:08:54+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements