Constructor Reference



interface WordFactory{
public Word getWord(String word);
}
class Word{
private String word;
public Word(String word) {
this.word = word;
}
public void printDetail() {
System.out.println("L-Case: "+word.toLowerCase());
System.out.println("U-Case: "+word.toUpperCase());
System.out.println("Length: "+word.length());
System.out.println("_____________________");
}
}
public class ConstructorReference {
public static void main(String[] args) {
WordFactory wordFactory1 = Word::new;
Word w1 = wordFactory1.getWord("dove");
w1.printDetail();

WordFactory wordFactory2 =  (str) -> new Word(str);
Word w2 = wordFactory2.getWord("Peacock");
w2.printDetail();
}
}
Output
L-Case: dove
U-Case: DOVE
Length: 4
_____________________
L-Case: peacock
U-Case: PEACOCK
Length: 7

_____________________