
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement a constructor reference with one or more arguments in Java?
In this article, we are going to learn about implementing a constructor reference with an argument or multiple arguments in Java. Constructor references in Java provide us with a concise way of creating new objects via method references. These are going to enable us to reference a constructor without running the constructor itself so the code looks cleaner and it will be more readable.
What is a Constructor Reference?
A method reference may also be used with Java 8 constructors. A constructor reference may be defined by employing the class name and a new keyword. The constructor reference may be passed to any functional interface reference defining a method with compatibility to the constructor.
Syntax
Following is the syntax for a constructor reference:
ClassName::new
Constructor Reference with One Argument
To make things more clear we are going to take an example of a situation where we have to create Student objects via constructor taking a single argument (name).
Example
In the following example, we will see how to implement a constructor reference with one argument. To make that happen, we will create a functional interface that has a method which takes a single argument and returns a Student object.
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; } }
Following is the output of the above code:
Adithya Jai Jai
Constructor Reference with More Arguments
Constructor references can also be used with constructors that take multiple arguments. In this case, we will create a functional interface that takes two arguments and returns a Student object.
We will use BiFunction<T, U, R> to achieve this.
Following are the steps to implement a constructor reference with two arguments:
- Create a functional interface with a method that takes two arguments and returns an object.
- Use the BiFunction<T, U, R> interface to implement the functional interface.
- Implement the constructor reference using the functional interface.
Example
Following 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; } }
Following is the output of the above code:
101 111 121
In 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.