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

Kyle Baker
Kyle Baker
8,211 Points

How come we can call class and then fields?

In this video, Ben calls the class ColorWheel with mColorWheel and then goes on to call the field mColors with the dot notation (mColorWheel.mColors). Why does this work? And is this a special case or will it always work like this?

2 Answers

Emily Conroyd
Emily Conroyd
6,512 Points

If you look above in the class there is a declaration and instantiation private ColorWheel mColorWheel = new ColorWheel(); [declaring new object ] [instantiating using keyword new] The reason this works is because we are accessing an object, mColorWheel, of type ColorWheel which has a property of an mColors array.

If you include an object of the type of class you want to access properties from, it should work every time.

Happy Coding! May the Force be with You

Boban Talevski
Boban Talevski
24,793 Points

I guess Kyle is confused because in java and android courses so far (along the android track) the member variables were mostly kept private and being accessed with getter methods, not directly like in this case. It felt a bit weird to me as well coding like that considering I got used to the "keep your member variables private" guideline.

So anyway, could we (and wouldn't it be better to) add getter methods in both FactBook and ColorWheel classes and call those getter methods when initializing mFact and mColor in FunFactsActivity?