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

Java Java Objects (Retired) Harnessing the Power of Objects Helper Methods and Conditionals

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,363 Points

Why no return value on the method load?

I guess I don't understand return value enough yet. To me there is a return value on the method load. What ever the pez count is, it will now return a value of 12. Can someone help me visualize this differently?

1 Answer

Aaron Kaye
Aaron Kaye
10,948 Points

Load is performing an action so there doesn't really need to be anything returned. You are loading pez candy in the Pez dispenser. A return would be something like getting the Pez characters name or the number of pez in the dispenser. Java is nice because you can tell by the method if something will be returned.

In our PezDispenser.java, the method is like this:

public void load() {
    mPezCount = MAX_PEZ;
}

Since we have the void we know that nothing will be returned. In our isEmpty we are seeking to get information from calling the method, we want the method to tell us if the Pez dispenser is empty or not. The method looks like this:

public boolean isEmpty() {
    return mPezCount == 0;
}

We can tell this will have a return value because (1) the method is no longer void, now it is boolean, meaning we will return a boolean value. (2) we also know it will return because it has the return keyword. The expression mPezCount == 0 is a comparison that will be either true or false and that is what will be returned. So just remember, the load method isn't looking to get information, it is just performing an action. Hope this helped!