Java Java Objects Harnessing the Power of Objects Filling the Dispenser


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.