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

Initializing the CrystalBall Object

Hi!

I had a question about the Simple Refactoring video for the Crystal Ball app.

I understand why we wouldn't want to initialize the CrystalBall object within the setOnClickListener method, but could we initialize it just one level up in the onCreate method (rather than outside it as suggested in the video)?

Thanks!

2 Answers

That really depends. If you are going to use the mCrystalBall outside the onCreate method then you can't. But if you are going to use it in that method only you can do it.General rule of thumb is that if several methods in that class accesses it you declare it at class, if only the method has use for it you declare them in methods. Since our CrystalBall application as far as I remember doesn't have any other method so far you can do it inside onCreate method. (Can't remember the end project though.)

Great, Hasan. That makes a lot of sense.

Thanks!