Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Meet Objects Welcome Back

Neros E.
Neros E.
528 Points

Having a really hard time getting my head around method chaining in java. Help appreciated.

I understand the results of it but not the concept itself, and no past post here or internet search seems to answer my question, so I’ll now lean on the TTH community now.

So my main question is WHY is it possible to chain methods? Is it because methods can become objects upon which you can call more methods?

Example: String myName = "Neros"; myName.toLowerCase().toUpperCase(); // returns "NEROS"

As I understood it methods can ONLY be called upon (|| exclusive to) objects, is this true? and does that mean that the entire expression MyName.toLowerCase(). Is an object? I vaguely read while searching that it’s because methods like toLowerCase delivers a string (in this case “neros”) which is an object, so it’s essentially chaining on the returned String objects of a method? is this correct or am I completely lost?

Additional questions: -If methods are not exclusive to objects, I wondered if you can call one on a String Class, even chain them? Example : String.add.subtract(3,1) = 2

-if yes where would you apply class method chaining?

-Do all methods need dot operators ? ----"String add(x,y)?

-Can you chain on primitive data methods/fuctions like parseInt();?

Again. any help is greatly appreciated. The more detailed the better!

Reference: I'm on the first video of the Java Objects course with Craig at m5:30 where he briefly mentioned method chaining.

2 Answers

Mohammad Laif
PLUS
Mohammad Laif
Courses Plus Student 22,297 Points

Methods are objects (everything is object even classes).

As I understood it methods can ONLY be called upon (|| exclusive to) objects, is this true?

You can call method on your object like myName.toLowerCase(). And you can call method without using object, by them self like printAwesomeName(); and you can call method on method, and calling method on class too, each object has its own method.

so it’s essentially chaining on the returned String objects of a method? is this correct or am I completely lost? Yep. For your information, in Java 8 there is something awesome called Stream doing exactly what you meant.

Additional questions: -If methods are not exclusive to objects, I wondered if you can call one on a String Class, even chain them? Example : String.add.subtract(3,1) = 2 From my knowledge each class has its own method, Also there is Reflections! (methods) I used them in Android Development. Examples to know my class name:

MainClass.class.getSimpleName();
MainActivity.this.getClass().getSimpleName();

if yes where would you apply class method chaining? from above example!

Sorry I could not understand the rest questions, but I had same questions when I start to learn programming, I could say to you just don't rush your self, or your mind will get overwhelmed.

Neros E.
Neros E.
528 Points

Thank you for your response, yeah I did get a little overwhelmed but I now understand method chaining and your answer just confirmed I was right!

Kalea Wolff
Kalea Wolff
2,672 Points

So, method chaining is the use of more than one method at a time. It will still follow the rules of using a method (dot notation). It allows you to be more efficient in your code later once you've built your own methods. But out of the box, you can chain methods together to do fun stuff.

Say we have a String as given in your example. You lowercased and immediately uppercased it. But we wanted to return it differently to the user (or server as is frequently the case). We can't often trust users to give us valid input. So in this case, let's fix it before we return it.

String myName = " Neros " ; Note the intentionally included spaces.

public String lowerChangeLetterAndRemoveSpaces (String name) { return name.toLowerCase().replace('N', 'H'). trim(); }

This will return 'heros' when we call. lowerChangeLetterAndRemoveSpaces(myName).

I know this was quick and dirty, but I hope it helps you.

Neros E.
Neros E.
528 Points

Thank you taking from your time to answer, though it didn't quite give what I was looking for, I'll try to ask the question again but much clearer.