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 trialYusuf Mohamed
2,631 PointsA 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?
2 Answers
Steven Parker
231,275 PointsThe 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 cooper
30,617 Pointsresponse 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
Yusuf Mohamed
2,631 PointsThanks for extra "resources" ba dum Tss
Yusuf Mohamed
2,631 PointsYusuf Mohamed
2,631 PointsBut if we already have access to the method why are we is the method getResources() set to the variable resources?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsYou have access to the "getResources" method by inheritance. When you use that method, you get access to the resources object.
Yusuf Mohamed
2,631 PointsYusuf Mohamed
2,631 PointsBut instead of doing
Why can't we do
String key = getResources().getString(R.string.key_name);
directly without setting the method to a variable?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsYou can do that. And if you are not going to need the resources for anything else later, that actually would be a good strategy to keep the code more compact.
Yusuf Mohamed
2,631 PointsYusuf Mohamed
2,631 PointsBut if I am going to need resources later why would it be a bad idea to do it my way?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsIf you'll be using it more than once, then assigning a variable the first time saves you from needing to call "
getResources
" again later. It's the "DRY" principle — "Don't Repeat Yourself".Happy coding!
Yusuf Mohamed
2,631 PointsYusuf Mohamed
2,631 PointsNow I understand, thanks for the help.