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 Methods and Constants

SirBralin Robertson
SirBralin Robertson
625 Points

Voiding the load()?

Why would we void the return value load? I understand that void means to not return a value. I just don't get why would I use it in this instance seen below:

public void load() { mPezCount = MAX_PEZ; }

4 Answers

The function you gave:

public void load() { 
    mPezCount = MAX_PEZ; 
}

has void for its return type, as you indicate, because it doesn't return anything, i.e., there is no return statement in the body of the function.

What the function does is set the value of the instance variable mPezCount to the value of MAX_PEZ. But initializing a variable is not returning anything.

Compare this (made-up) function:

public boolean load() { 
    mPezCount = MAX_PEZ; 
    return true;
}

Here the return type is boolean because the function returns true.

You are welcome. Hope it helped.

SirBralin Robertson
SirBralin Robertson
625 Points

Yes, it did. Also, I just sat for a minute to think about the concept of the object (Pez Dispenser).