What is constructor reference in java 8?
As you know constructors are kind of special methods, method reference will also applicable to constructors. Constructor References can be created using the Class Name and the keyword new with the following syntax.
Syntax :
1 |
<Class Name>::new |
Example :
1 2 3 4 |
String::new; Integer::new; ArrayList::new; UserDetail::new; |
The constructor reference for all the below lambdas are Student::new and type of the constructor invocation will be decided based on the target type. To understand it better we will see a use case whose goal is to return a Student instance but the String and int type will be supplied as method argument(s). These use cases are not better programming standards but to understand how Constructor reference work.
Example with no arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
@FunctionalInterface interface MyFunctionalInterface1 { Student getStudent(); } // class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } // public class ConstructorReferenceDemo { public static void main(String[] args) { MyFunctionalInterface1 mf = Student::new; Supplier<Student> s1 = Student::new;// Supplier Example Supplier<Student> s2 = () -> new Student();// equals to above line System.out.println(mf.getStudent());//Student class toString() call System.out.println(s1.get());//Student class toString() call } } |
Example with one argument:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@FunctionalInterface interface MyFunctionalInterface1 { Student getStudent(String name); } // 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; } } // public class ConstructorReferenceDemo { public static void main(String[] args) { MyFunctionalInterface1 mf = Student::new; Function<String, Student> f1 = Student::new;// Function Example Function<String, Student> f2 = (name) -> new Student(name);// equals to above line System.out.println(mf.getStudent("Peter").getName());//Peter System.out.println(f1.apply("Gerhard").getName());//Gerhard System.out.println(f2.apply("Gerhard").getName());//Gerhard } } |
Example with two arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
@FunctionalInterface interface MyFunctionalInterface1 { Student getStudent(int id, String name); } // 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; } } // public class ConstructorReferenceDemo { public static void main(String[] args) { MyFunctionalInterface1 mf = Student::new; BiFunction<Integer, String, Student> f1 = Student::new;// Function Example BiFunction<Integer, String, Student> f2 = (id, name) -> new Student(id,name);// equals to above line System.out.println(mf.getStudent(10, "Peter").getName());//Peter System.out.println(f1.apply(20, "Gerhard").getName());//Gerhard System.out.println(f2.apply(30, "Gerhard").getName());//Gerhard } } |