Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.