There is a A Class
class A {
private String str;
public void setStr(String str) {
this.str = str;
}
}
- this method works fine;
public static void main(String[] args) {
A a = null;
Optional.ofNullable((String) null)
.ifPresent(str -> a.setStr(str));
}
- This method Causing NullPointerException
public static void main(String[] args) {
A a = null;
Optional.ofNullable((String) null)
.ifPresent(a::setStr);
}
Why?
Those two methods should be no difference in my perspective;
How this happend?
New contributor
周亿进 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.