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

Ashford Morgan
Ashford Morgan
1,973 Points

Having trouble understanding placement of mPezCount...

I don't understand why mPezCount is placed inside the constructor...

public PezDispenser(String character) { mCharacter = character; mPezCount = 0; }

because PezDispenser's parameters are a String Character i can't see why an Integer thats not equal to the character variable would be in there. Is it just for the sake of adding properties to the PezDispenser when it is initialized?

Maybe i'm thinking too much idk, thanks.

1 Answer

Isaiah Marin
Isaiah Marin
11,971 Points

Hey Morgan, from what I understand a "Constructor" is invoked when you make your class object and use the "new" operator.

Ex. PezDispenser dispenser = new PezDispenser("Yoda");

In this case, the constructor allows the user to give her/his PezDispenser a name while going ahead to initialize its Pez count. I like to look at it as using the constructor to get everything setup. Thus, its convenient to go ahead and initialize the mPezCount to a standard value of zero (since there are other methods to use for loading & such). That way, after we do make the object and call the constructor, we'll able to use the other methods such as isEmpty() or dispense(), etc.. Since we know everything we needed to have a value has been set.

This is how I look at why the Professor had told us to give the value of mPezCount 0 in the constructor. Even though it only takes one String argument. I hope this helps.

Ashford Morgan
Ashford Morgan
1,973 Points

Ok yea that was kinda what I thought too. Thank you for the response!