File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Explanation o f concatenated method calls/cascaded method calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Explanation o f concatenated method calls/cascaded method calls" Watch "Explanation o f concatenated method calls/cascaded method calls" New topic
Author

Explanation o f concatenated method calls/cascaded method calls

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 256
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

Joined: Jul 17, 2002
Posts: 256
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.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jul 17, 2002
Posts: 256
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

Joined: Mar 22, 2002
Posts: 1055
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.
David Brafford
Ranch Hand

Joined: Feb 11, 2001
Posts: 91
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Explanation o f concatenated method calls/cascaded method calls
 
Similar Threads
constructor chaining?
Java help!!!!
super & this
Using System.currentTimeMillis() method
classes and methods