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 trialRami Awar
5,435 PointsI have an important conceptual question. Why did we create the CurrentWeather class and not just use variables?
1- I tried first of all, to make a toast inside this asynchronous process were making, and my app crashed several times. 2- Then I tried storing the json data in a variable and issuing the toast, but the info took time to come in so the variable assignment process was wrong. 3- I realized that all the currentweather class is doing is kind of similiar to what a variable does. You set it, the you call it. So why are we doing this using a class? Thanks in advance for clarifying LIFE. :p
1 Answer
Taiwo O
2,173 PointsI'm no expert so don't take my answer as 100% accurate. To answer your question: You know that variables store information; A String stores letters, an int stores numbers, etc. However if you want to store more abstract data such as a current weather, then we have to create a class and define all of its properties. This way once we import and declare the class (CurrentWeather current = new CurrentWeather() ) then it acts just like a variable.
Also by making it into a class, we can reuse the class multiple times to create multiple current weathers if you wanted to. Using a class makes this easy, all I have to do to create two current weather variables is: CurrentWeather current1 = new CurrentWeather(); CurrentWeather current2 = new CurrentWeather();
Hope this helped
Rami Awar
5,435 PointsRami Awar
5,435 PointsIt does add a strongpoint, but I still couldn't use variables instead of it when I tried. If classes were only shortcuts then I could have used the variable normally. But here comes the "threads" problem. Were not working on the main thread when getting the data and this variables used are causing crashes. And when I declare a toast in the OnResponse part of the request for data, the app crashes for example. This stirred up many questions.