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 Incrementing and Decrementing

Anas Rida
Anas Rida
8,183 Points

declaring the boolean inside the method

why was the boolean wasDispensed not declared outside of the dispense() method??? in other methods like isEmpty() and load(), the mPezCount is declared outside. Can someone please clarify this for me. Thanks in advance

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Since it's value is only needed inside the dispense() method it is best to declare it as a local variable, inside the method. Knowing whether one pez was dispensed or not has more to do with the act of dispensing a pez, hence used inside the dispense method, than the pez dispenser in general. On the other hand, the value of mPezCount is used in more than one place and is a fundamental property of a pez dispenser so it is declared as a member variable.

Anas Rida
Anas Rida
8,183 Points

thank you very much. I understand this now