Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?