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 Android Activity Lifecycle The Activity Lifecycle Retrieving Instance State

Initializing variables in constructor

Hi there,

in this video, Ben initializes mColor and mFact directly when declaring the variables. Is there any difference if I initialize them in the constructor? By the way, is it bad practice to use constructors in Android activities?

Thanks in advance,

Philip

Ben Deitch Ben Jakuben

Mazen Halawi
Mazen Halawi
7,806 Points

It shouldnt matter, its good practice to initialise the variables so they dont hold null values and crash the app if you forget to check for null. You can consider initializing the variables as giving the default values and if you need to change those default values you can simply send new values through parameters into a function.