I have a class containing nested and inner classes. And in some program, i am check for the null values of the object using equal operator in the legacy java way. The following is my code snippet:
class Outer {
Nested nested;
Nested getNested() {
return nested;
}
}
class Nested {
Inner inner;
Inner getInner() {
return inner;
}
}
class Inner {
String foo;
String getFoo() {
return foo;
}
}
And here is how i am doing the null checks on the reference variables:
Outer outer = new Outer();
if (outer != null && outer.nested != null && outer.nested.inner != null) {
System.out.println(outer.nested.inner.foo);
}
I know this can also be done using the Optional class of java 8, but i am not getting a way for doing the same above particular scenario. Any suggestion?