Java 8 — Constructor Reference | Code Factory

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();
}
}
Sample.Sample()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store