You can do this.
However I don’t think it’s good practise.
- my first concern is the potential for null pointer exceptions (NPEs). If you write
a.getB().getC().getD()
, any one of these can return null. You can’t tell which returned null in the resulting stack trace (excluding the last invocation returning null, which is fine). You may wish to use the null object pattern in this scenario. - my second concern is more design-oriented. You’re asking A for info about what it knows, then asking that result, and so on and so forth. Objects exist to do things for you, so you should do just that, and let your objects work for you. Get your
Person
object to do the heavy lifting.
2
solved Java two get() methods after each other [closed]