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

Lakshmi Narayana Dasari
Lakshmi Narayana Dasari
1,185 Points

what is the use of void load() method in this program?

As load() method can't return anything when its called , what is the use of this method?

1 Answer

James Vaughan
PLUS
James Vaughan
Courses Plus Student 4,462 Points

Hi Lakshmi,

There are two load methods within the PezDispenser class. These are:

public void load() {
    load(MAX_PEZ);
}

and

public void load(int pezAmount) {
    int newAmount = mPezCount + pezAmount;
    if(newAmount > MAX_PEZ) {
        throw new IllegalArgumentException("Too many Pez!");
    }
    mPezCount += pezAmount;
}

Although they are both called load, they are different. This is known as method signatures. Essentially the combination of the name and the parameters it is called with. With regards to the above two, the first is called to load an empty dispenser with a full set of Pez. This is the same as calling:

dispenser.load(12);

I agree that it is likely redundant in the code, but is a good demonstration of the use of constants and method signatures which is the concept of this kind of tutorial. Potentially it could be refactored.

Hope this helps