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 Simple Android App (2014) Improving Our Code Simple Refactoring: Using a Class

Liron Tal
Liron Tal
4,825 Points

Why not creating mFactBook after onCreate?

This seems confusing, since it sounds logical to create the object when the Activity is being created.

Please explain :)

Liron

3 Answers

Harry James
Harry James
14,780 Points

Hey Liron,

Awesome question! The reason we defined it at the top is all to do with scoping.

We could set the variable inside onCreate() but if we were to do so, our mFactsBook variable would be stuck in the method scope, which limits what we can do with it.

Alternatively, we could always declare it in the class scope but initialize it in the onCreate() scope:

public class FunFactsActivity extends Activity {

  private FactBook mFactBook;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    mFactBook = new FactBook();
  }

}

This is perfectly acceptable but, it does take up two different lines of code when there's really no need to, so the primary reason for initializing the variable in the class scope is just to clarify our code and keep it clean.


Hopefully this should explain why this was done but if there's still something that you don't quite get, let me know so that I can dive into a bit more detail for you ;)

Liron Tal
Liron Tal
4,825 Points

So can it be minimized to 2 lines?

Thanks Liron

Harry James
Harry James
14,780 Points

Hey again Liron,

Yes, the code sample I provided in my answer will do this in two lines. This works but it's still clearer to declare and initialize on the same line.

Let me know if you have any more questions :)

Liron Tal
Liron Tal
4,825 Points

Ok so let me ask you this,

if you'd put this into one line:

private FactBook mFactBook = new FactBook();

Will it be inside or outside onCreate? What's the best practice?

Thanks again Liron

Harry James
Harry James
14,780 Points

Hey again Liron!

We wouldn't actually be able to use the private keyword inside onCreate() - it's not allowed in the syntax of the language to put access modifiers in methods.

The best practise would be to include that line outside of the onCreate() scope but inside the class scope, this is where we put our member variables - usually at the top for easily visibility.


Again, if there's still something that doesn't quite click just yet, let me know :)

Liron Tal
Liron Tal
4,825 Points

Thank you!

Liron