- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
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
Example of 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<Sttring, 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
Example of 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
- Related Articles
- How to use one or more same positional arguments in Python?
- How to create a constructor reference for an array in Java?
- Differences between Method Reference and Constructor Reference in Java?
- How are arguments passed by value or by reference in Python?
- How to pass arguments by reference in a Python function?
- How to increment one or more counters with JavaScript?
- How to implement LongPredicate using lambda and method reference in Java?
- How to implement LongSupplier using lambda and method reference in Java?
- How to implement DoubleUnaryOperator using lambda and method reference in Java?
- How to implement DoublePredicate using lambda and method reference in Java?
- How to implement LongBinaryOperator using lambda and method reference in Java?
- How to implement LongFunction using lambda and method reference in Java?
- How to implement DoubleSupplier using lambda and method reference in Java?
- How to implement IntToDoubleFunction using lambda and method reference in Java?
- How to implement IntToLongFunction using lambda and method reference in Java?

Advertisements