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

Android Build a Weather App (2015) Working with JSON Setting CurrentWeather from JSON

Why can we not string methods with our CurrentWeather Object just like the AlertDialog.Builder Object from earlier?

Question says it all. If you could please explain why and also how we could allow our own class to allow its objects to string methods that would be wonderful.

For example:

// Non string of methods

currentWeather.setTime(long);

currentWeather.setTemp(double);

//Stringing of methods

currentWeather.setTime(long).setTemp(double);

1 Answer

Joe Brown
Joe Brown
21,465 Points

You can design the class to follow a design pattern that allows you to do that. Look up the "Builder" pattern for java and you will get the idea. Android uses it in different places, like the AlertDialog you mentioned. Like any pattern it should be used only as needed. If you happen to have access to the book Effective Java he explains it well with an example in item 5 I think it is .