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 Harnessing the Power of Objects Filling the Dispenser

pezCount

What is the need to set pezCount = 0 when it is not in the scope of the constructor. Also why isn't it initialized in the class itself.

2 Answers

hi, u set it to 0 because we simulating the build process of a pezDispenser, in the start u set that every pezDispenser will have 0 pez in it until we call the fill() method in order to fill the pez.

Actually we have to initialize the pezCount otherwise we will get an error that it is uninitialized. I'm not sure but I think that initialization on the constructor is better if you want to use some kind of logic (loop, if etc.) on the constructor.