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

Android Development Stage 2: Harnessing the Power of Objects. Why do we need boolean wasDispensed in PezDispenser.java?

'''javascript

public boolean dispense() { boolean wasDispensed = false; if (!isEmpty()) { mPezCount--; wasDispensed = true; } return wasDispensed; }

'''

Why do we need to say 'wasDispensed = false' and then what are we returning when we 'return wasDispensed'? True? It seems unnecessary.

3 Answers

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

You set it to false because at some point it will be false. Your code is what will happen to it once it is false so that it will become true. If you only add the code as true or if the value remained true forever, then you would not need the code that follows IMHO. Happy Coding :)

My problem is that we created a variable 'wasDispensed' and only used it within this method 'dispense()'. It doesn't come up again in the code so to me it seems insignificant unless we write a later statement that is dependent upon 'wasDispensed == true/false'. The fact that we set 'wasDispensed = false' also bothers me because at the end we set it to true so whenever this code runs it will always return 'wasDispensed = true', so even that first line setting it to false initially seems unnecessary. If anything that should be set at the start of the code as a public variable outside the statement. Also saying 'return wasDispensed' afterwards returns what? I take it to just be returning true.

Lucas Santos
Lucas Santos
19,315 Points

I actually had a hard time understanding this one as well but after re reading the code dozens of times and trying to understand why wasDispensed was false as it's default value I understood. I believe it is because of the while loop. Because the way the while loop works is:

while(condition in here is true keep running the code){
//code running
}

so the if statement in that dispense method says if is not empty then remove a pez with mPezCount-- which will make the wasDispensed true. But when mPezCount is 0 the if statement in the dispense method is no longer valid and wasDispensed = false (it's default value) making the while loop false and not run the code.

Jesse Daniels
Jesse Daniels
810 Points

You've got the idea down in your answer, but you need to remember that there isn't always going to be a while loop used with the code. Do you always eat all of the pez in one sitting? Sometimes you're going to want to eat just one (or maybe just two). The while loop was just supposed to illustrate that the code works.

The point of setting wasDispensed to false in the beginning is so that we don't assume the pez dispenser is always full and that it has dispensed (this could cause some errors if it were empty and then we called this code and it returned true, despite being empty and unable to dispense). We then test the state of the pez dispenser to find whether it can dispense or not; this is the purpose of the if statement. If we find that it's not empty, we subtract one of the pez and set wasDispensed to true to reflect this. If it is empty, therefore unable to dispense, then we don't need to do anything because wasDispensed is already false, and that's what will be returned.

I think what the person who asked this question is failing to understand is that the state of wasDispensed is dependent on the test we do with the if statement. I hopefully cleared up why the code is the way it is with my previous paragraph.