• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explanation o f concatenated method calls/cascaded method calls

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book I am using isn't doing that good of a job explaining this and its the first time I've ever seen it used before. Here is the example code they provided:

What is confusing me is the statement as I've never encountered that specific statement. Why is there a return statement for these mutator methods (beyond the fact that the return type requests it)? I don't see the logic. In my experience I've always typed those void.
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is that class being applied. This explains it a bit better as I can see the code but I still don't see the point and am a bit confused on the logic.


My quibble is why line 18 when a constructor could be used at declaration/initiliaztion that does the same thing??? But I'm still confused as to what is happening here.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need it to do something like this:
SomeClass s = new SomeClass();
s.doA().doB().doC();
assuming the methods doX() return this. It's evaluated in this order:
( ( s.doA() ).doB() ).doC();
so the thing inside the parenthesis must be a SomeClass object in order to invoke the methods. In other words, the return value of doA() is a SomeClass object (because of this) so you can invoke doB(), which in turn returns the same (but possibly altered) object, which allows you to invoke doC().
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anthony Villanueva:
You need it to do something like this:
SomeClass s = new SomeClass();
s.doA().doB().doC();
assuming the methods doX() return this. It's evaluated in this order:
( ( s.doA() ).doB() ).doC();
so the thing inside the parenthesis must be a SomeClass object in order to invoke the methods. In other words, the return value of doA() is a SomeClass object (because of this) so you can invoke doB(), which in turn returns the same (but possibly altered) object, which allows you to invoke doC().



Does the returned this object have the member variables accessed/changed by the doX method i.e. doA returns A so this has A but then calls doB which returns B so this now has A and B?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matt Kidd:
Does the returned this object have the member variables accessed/changed by the doX method


Yes. See the sample code below:

We know it has to be the same object because the final output is 6.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see the point of returning
this on the set methods. For one, it
differs from the Java Bean signature of
having set methods with void. Thus, using
these classes in a Web application or beans
in jsp files would be non intuitive.
If your question is on stringing multiple
methods - and the context is demonstrating
the use of stringing multiple method calls
together, I would use the getXXX() method
rather than setXXX as the example. The Singleton
pattern uses the getClassName() method for
a class of type ClassName. So if I have
method doWork() in class Order, I might
see code like:
Order.getOrder().doWork();
Order.getOrder() returns a reference to an
Order object. getOrder() is a static method
in Order that returns the private reference
within the Singleton object. Now that I have a
reference, I can invoke a method ( just like I
was using the implicit "this" reference ).
------------------------------------------
Considering the Certified Java Programmer Exam?
Get JCertify!
http://www.enterprisedeveloper.com/jcertify
The best investment in your career you will make all year
 
I child proofed my house but they still get in. Distract them with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic