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 trial

Java

Chaining Methods

I want to know more about chaining methods to one object in java. like this sentance.toLowerCase().contains("hello")

I use this peace of code in one of my small applications and it worked fine. Is there anything I should consider when I chain methods together like this. I am trying not to miss anything. Please let me know if I am missing anything.

This isn't necessarily an answer, but just try to keep in mind the return type of each method. in each method you used in the example above, you started with a String and returned a String each time up until your contains method which returns a boolean value. so if you were to try to chain another method that belongs to the string class after your contains method, it wouldn't compile. theoretically you can continue chaining methods as long as the return type of the previous matches the method you are trying to implement.

1 Answer

Thanks Anthoney