• Post Reply Bookmark Topic Watch Topic
  • New Topic

method chaining  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I changed my code a little bit below I think it's more efficient anyway my question is how does this line of code actually work

p.getOne().sayHello();

coming from C++ this line of code looks very foreign I understand the p.getOne() part I'm just calling a method from the p object but how come you can then add another . after it and call another method from a method? I maybe would understand if I was calling another objest inside a method like System.out.println() but how can we actually call a method from a method like the code in question,how does this piece of code work,

thanks guys

main.java



person.java



Birthday.java

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the unlikelihood of a birthday saying anything, least of all Hello, or of being called one . . .

Have a look at this thread, where that sort of call is discussed.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:

coming from C++ this line of code looks very foreign I understand the p.getOne() part I'm just calling a method from the p object but how come you can then add another . after it and call another method from a method?


Coming from C++, why would this line of code look very foreign to you?  C++ supports function/method chaining too.

... although, admittedly, if you are method chaining with C++, you are also very likely to be using pointers too. So, you will more likely be doing this...



... but both are valid with C++.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Apart from the unlikelihood of a birthday saying anything, least of all Hello, or of being called one . . .

Have a look at this thread, where that sort of call is discussed.


haha true I used the code for something else before another example of composition which required a birthday object but since I'm not making a program just learning some concepts I didn't change the name just the functionality albeit yes it's probably not the best idea.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . since I'm not making a program just learning some concepts I didn't change the name . . .
In view of the strange variable names we see on this forum, we think it best to query all such names because we don't know whether the poster knows they are using a peculiar name or not.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:haha true I used the code for something else before another example of composition which required a birthday object but since I'm not making a program just learning some concepts I didn't change the name just the functionality albeit yes it's probably not the best idea.

You may not think that names matter much when you're learning but I think that choosing poor names really hinders your ability to grasp even basic concepts.  Try doing the Stroop Effect test

Symbols and their meanings are very much tied to how well your brain can navigate and wrap itself around new concepts. Imagine that you were back in high school trying to learn algebra.  How difficult would it have been if your teacher had given you the following:

SymbolMeaningExample
+equals1 + 1
Baddition1 B 2 + 3
Applesubtraction2 Apple 1 + 1


If you think the table above is ridiculous, go back and read your code again and see if it even makes sense.

(EDIT: And you can see by the number of edits I've had to make to this post, even I am confused by my own example)
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point Junilu

Hey Campbell I just read that thread you pointed me to but it didn't answer my question and when I googled method chaining I got little to no answers the little was on javascript =(

the question I was asking is how is p.getOne().sayHello(); legal?

I would understand if P.one.sayHello() would be legal IE calling a member object of P then calling that objects method but I don't understand how a method can call another method in this way.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example what would be the difference of using p.one.sayHello() both seem to do the same thing when would you choose one over another and why would you?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
the question I was asking is how is p.getOne().sayHello(); legal?

I would understand if P.one.sayHello() would be legal IE calling a member object of P then calling that objects method but I don't understand how a method can call another method in this way.


A method is *not* calling another method here. The getOne() method is returning an object -- specifically, a Birthday instance. And this Birthday instance is what is used to be deferenced for the sayHello() method.

And as a side note, since you mentioned C++, but seemed to have ignored my previous post.... this is the exact same behavior as in C++ too.

Henry
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does getOne() return? What type is the expression just before you get to the . after it? What difference is there betweenandwhich I copied and changed from the th‍read I quoted earlier?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Henry thanks for the reply and yeah that actually makes perfect sense I wouldn't say I'm fluent or even near fluent but I have a much better grasp with C++ then I do Java,I never have heard of method chaining this week which is pretty crazy,the reason behind my move back to Java is the user friendly swing and awt when it comes to GUI design where as making a GUI application in c++ is far far more complex.

and also thanks for the reply Campbell much appreciated I think in the example you gave if the OP would have used the full code in his example it would have being easier to decipher.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
** until this week
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:for example what would be the difference of using p.one.sayHello() both seem to do the same thing when would you choose one over another and why would you?
That breaches encapsulation by allowing direct access to a field. Use of a getXXX method can maintain encapsulation by not allowing the field to be reassigned.

Sorry for delay: I thought I had posted this reply yesterday.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem thanks for the reply
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that makes sense =)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!