I have a use case, where I have nested classes and an object of the top class. I want to get a value which is at the Nth level. I\'m using getters repetitively to achieve th
Wrap each nested class in an Optional:
Class A {
    String a1;
}
Class B {
    Optional a;
}
Class C {
    Optional b;
}
Class D {
    Optional c;
}
 
Then use flatMap and map to operate on these optional values:
String a1 = d.flatMap(D::getC) // flatMap operates on Optional
             .flatMap(C::getB) // flatMap also returns an Optional
             .flatMap(B::getA) // that's why you can keep calling .flatMap()
             .map(A::getA1)    // unwrap the value of a1, if any
             .orElse("Something went wrong.") // in case anything fails
You might want to check out the concept of Monads. And if you're feeling adventurous, Scala is too distant from Java.