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

Can someone explain why we need DAILY_FORECAST and what it is for.

I cant get my head around why we need these static methods. how does this help us to pass data when it is just an ordinary String?

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

When you are passing data to a new Activity, you need to add it to the Intent you use to start it using what are called Intent Extras. These are key/value pairs, much like entries in a Map, with a String for the key. So DAILY_FORECAST will serve as the key, and the forecast data as the value.

So, why a static constant, and not just a string literal? Since the string is needed to both store and retrieve the data it's best to refer to a single constant and not copy/paste the same string over and over. That way it you need to change it, you only change the static constant, not multiple lines of code with the possibility of missing one. It's best practice, especially when values will be used in more that one place. It keeps constant values in one place near the top of the code where they can be easily changed instead of hard-coded across the code.

Thanks! that explains it :)