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 (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,732 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