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

All About @strings Objects

When creating the resources object, it seems like we are setting on object, 'resources' equal to a method, 'getresources().' is this true? can we make an object equal to a method? Isn't a method a property of an object? I guess what I am also asking is why don't we great an instance of the Resources object/class and then just use the methods available from that object? I just learning java so I am straightening out these ideas.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Steven, great question! Let's look at an example line and talk about how it works.

Resources resources = getResources();

So as you mentioned, the left side declares a variable that is a Resources object named 'resources'. The equals sign means that we are assigning some value to the variable. But it's not assigning a method as you said in your question. When a method is called like this, the code for the method runs before the assignment takes place. The code in getResources() creates another Resources object, and then it returns that object. Here's an example of how the getResources() method is defined:

public Resources getResources() {
    Resources theResources;

    // Code to get and assign the resources

    return theResources;
}

So here you can see that a new Resources object is created and then returned. When it's returned, it essentially replaces the method call on the right side. So our code, while it's executing, ends up looking like this:

Resources resources = theResources; // When the return value gets substituted

Our code doesn't actually change, of course, but that's basically how it's working. So our object ends up getting assigned the value of the theResources object. Then our own code continues.

If you're wondering what happens to the theResources object that was created in getResources(), it gets flagged for garbage collection and will eventually be deleted and its place in the computer's memory will be available for new use. That's a whole other discussion. :)

As a side note, lots of students have asked about resources for Java, and I plan on covering that sooner than later. In the mean time, check out a game called CodeSpells, where you learn Java by creating "spells" out of code. It's pretty awesome.

Ben,

Thanks for the great explanation. I think I get this. That is a cool way to use methods. I will tuck this away.

I do look forward to seeing what else Tree House rolls out. Now that lunch has rolled around, I am going to check out CodeSpells. Thanks for the reference!