Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialParamvir Singh
1,517 PointsCascading Method calls
I was just wondering if there is any special way of reading cascading method calls so it can be easily understood by us. Like Craig tells us to think of array indexing as baby years. So, is there any neat little trick or something of that sort than can be used by us?
Paramvir Singh
1,517 PointsActual I had an idea what cascading method calls were and saw Craig use it so I though if there was an easier way to visual it.
1 Answer
Daniel Hartin
5,311 PointsOkay I see, where he used String.toLowerCase().split();
Cascading methods or method chaining is a process where the method returns an object (usually the same one but not necesary) an instead of us using a standard variable declaration, assignment and then calling a method we simply call the method off the returned object without actually holding it in another variable.
This is extremely useful at times when you have an object that you may want to make multiple state changes to but you dont want to make lines and line of code.
What I sometimes do is create an object like the below
public class FancyObject{
private final String mName;
private String mColour;
private int mHeight;
public FancyObject(Srtring name){
this.mName = name;
}
public setColour(String colour){
this.colour = colour;
return this;
}
public setHeight(int height){
this.mHeight = height;
return this;
}
}
public class AnotherBoringClass{
//Standard way
FancyObject obj;
obj = new FancyObject("myFancyObject");
obj.setColour("blue");
obj.setHeight(25);
//Using method chaining
FancyObject obj = new FancyObject("myFancyObject").setColour("blue").setHeight(25);
}
It's not always the best approach and sometimes calling multiple chained methods gets messy but I do really like it for things such as this and it is used lots throughout Java and the Android SDK more I find.
There is no right or wrong way it's whatever works for you. There is no easier way to know the methods unfortunately you are simply calling multiple methods off the returned object and if you are unsure it to the documentation unfortunately and if you do end up switching between multiple classes it can get confusing as to which class you're actually working on but most IDE's help with this and code completion is a god send :)
I'd wait until you move onto IntelliJ to see the real benefits of method chaining but I hope the above example shows you what is happenign behid the scenes. Normally a setter method wouldn't return anything but if instead pass back a reference to the object you can chain the methods together (or not - it's up to you).
Hope this helps some Daniel
Paramvir Singh
1,517 PointsThanks a lot for your help, I actually found the answer to my question. Once again thanks a lot.
Daniel Hartin
5,311 PointsDaniel Hartin
5,311 PointsHi Paramvir,
I think you've linked this question to the wrong video. I'll happily explain cascading method calls but it's hard when I have no context to base them on. If you can provide a code example or link to a video in which this is explained I'll try and elaborate further on the subject.
Daniel