DEV Community

Discussion on: How to Make Your Reviewer Cry Using Java Optional

Collapse
 
thegoncalomartins profile image
Gonçalo Martins • Edited

Not like you described, but like this

System.out.println("The number is: " + number.orElseGet(NameOfTheClass::alternativeNumber));
Enter fullscreen mode Exit fullscreen mode

With method reference.

If the method was not static and was an instance one, then it would be like this

System.out.println("The number is: " + number.orElseGet(this::alternativeNumber));
Enter fullscreen mode Exit fullscreen mode