import java.util.Optional;
public class Student {
public long id;
public String firstName;
public Optional<String> middleName;
public String lastName;
.
.
.
public Optional<String> getMiddleName() {
return middleName;
}
}
Student student = new Student();
if (student.getMiddleName().isPresent()) {
System.out.println(student.getMiddleName().get());
}
The above code will get an NullPointerException
since the default value of an Optional class member value is null. Should it be that the default value is Optional.empty()
?