Java 8 - Constructor Reference By using :: (Double Colon) Operator

Constructor Reference By using :: (Double Colon) Operator:

Rule:
* Constructor and method argument should be match.

* It will return Sample object, this is my expectation.
interface Interf{
public Sample get();
}
Class Sample{
Sample(){
System.out.println("Sample constructor");
}
}
Using Lambda:
Class Test{
public static void main(String args[]){
Interf i = () -> {
Sample s = new Sample();
return s;
};
Sample s = i.get();
}
}
Using Constructor ::
Class Test{
public static void main(String args[]){
Interf i = Sample :: new;    // Use this constructor.
Sample s = i.get();
}
}

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab