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.
A java logic problem.
So in this part of the video we need to access a method from the Resources class.
So I thought you would have to initialize it first like this
Resources resources = new Resources;
and afterwards you have access to all the methods inside that class.
Instead in the video he just created and variable and had access to the variable directly like this
Resources resources = getResources();
Am I missing something?
Steven Parker210,423 Points
The resources you want to access already exist, so you don't want to create a new (empty) object here. The "getResources" method is inherited from the context class, and returns a reference to that existing resource object.
alastair cooper28,199 Points
response to: Why can't we do - String key = getResources().getString(R.string.key_name); - directly without setting the method to a variable?
You can do that.
The difference in required computing power is quite small
If you are using them a lot, it saves a bit of typing
see this discussion for more on this https://stackoverflow.com/questions/28446653/whats-more-efficient-storing-variable-references-vs-not-context-in-android