2

In object oriented based programming language like java we can call a method of object by using objectName.methodName() -

aStudent.getName().equals(anotherStudent.getName()); 

Here to achieve the equals() method I have to use two dots (.). In some case there might be more than 2 dots like -

objectName.methodName1().methodName2().methodName3().methodName4()

My question is - Is there any limitation of such level of method calling ?

Mehmood Arbaz
  • 285
  • 1
  • 3
  • 11

2 Answers2

4

There is no technical limit, since writing:

result = foo.bar().baz();

is equivalent to writing

bar = foo.bar();
result = bar.baz();

There is, however, often a design limit: if you are accessing the child of a child of a child of ... an object, you might be violating the Law of Demeter (and this article might clear things up further, in case you're interested.)

mk.
  • 11,360
  • 6
  • 40
  • 54
2

As long as the method return an object, you can call any public method of this object, so no there is no limitation (none that you should matter.. there is always a limit).

Note that the approach may have no limit, but is not recommended at all, as it is harder to read, and harder to validate in one of the object is null.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76