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

Chaz Hall
Chaz Hall
1,970 Points

Confused on load()

public class PezDispenser{ public static final int MAX_PEZ=12; private String mCharacterName; private int mPezCount;

public PezDispenser(String characterName){ mCharacterName=characterName; mPezCount=0; }

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

public void load(){ mPezCount=MAX_PEZ; }

public String getCharacterName(){ return mCharacterName; } //blueprint||class that we can use to finish modelling the Example.java }

Under: public void load(){ mPezCount=MAX_PEZ;} I don't understand what the load() is actually doing. Is a load() just a name or is it something built into a package that is already running in workspaces? Can you guys explain what exactly it's doing in this code? Thanks!

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Chas,

load is a method. When you call this method the Pez dispenser will be filled with sweet pez to a maximum. To programm this in java you define a number how many pez can be stored inside the pez dispenser to fill it up completely. This number is limited, because Pez dispenser is a physical object and only a limited number can be be placed insde of it. So this number is constant. Makes sense ...

You create a constant this way:

public static final int MAX_PEZ=12;
// see the "all letters uppercased" variable name 
// this is a convention how you name constants

Now we know the maximum possible number of pez that can be stored inside a Pez Dispenser.

So when you call the load() method the number of bars inside mBarsCount (this is an integer variable that represents the state of Pez Dispenser (how many pez are inside dispenser right now)) will be set to the value of MAX_PEZ.

After calling the load method the Pez dispenser is full again. And you can enjoy the pez sweetness again and again. And if the dispenser is empty .... call load()

:smiley:

Grigorij