Java 8 — Constructor Reference | Code Factory

Index Page : Link

Reference Link : Link

Donate : Link

package com.codeFactory.methodAndConstructorReference;class Sample {
Sample() {
System.out.println("Sample.Sample()");
}
}
interface Interf {
public Sample get();
}
// Using Lambda Expression
public class Test {
public static void main(String... args) {
Interf i = () -> {
return new Sample();
};
Sample s = i.get();
}
}
// Using Constructor Reference
public class Test {
public static void main(String... args) {
Interf i = Sample::new;
Sample s = i.get();
}
}

Output :

Sample.Sample()

Syntax for Constructor Reference :

  • classname :: new