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

hakan Gülay
hakan Gülay
8,489 Points

boolean was dispensed=false

Hi, ı couldnt understand what ''boolean wasDispensed=false;" does ? and also wasDispensed=true? actually ı couldnt understand this video. can anyone explain to me ? thx.

2 Answers

Brendon Butler
Brendon Butler
4,254 Points

A boolean stores a value that's equal to true/false or 1/0.

Let's look at the information you provided. "boolean wasDispensed = false;"

The first word in the sequence is boolean, this is the variable type. Sort of like String or int. You need this to tell the compiler what type of variable you're about to assign.

"wasDispensed" is the variable name. This can be almost anything you want. Instead of "wasDispensed" you could type "dispensed" or even something totally irrelevant such as "isTheForceWithYou". This is bad practice, and you should keep your variable names as relevant as possible.

the = sign is an assignment operator. Basically telling java to set the variable to the value that you put after it.

"false" is one of the states that a boolean has. Instead of "false", you could use "true"

So knowing all this, "boolean wasDispensed = false;" would read as "Set the boolean variable wasDispensed to the value of 'false'".

Hopefully that clears things up a bit. If not, think of it this way. Imagine a box, this box is empty and we have a boolean variable "isEmpty". Since the box is empty, you would set the value of that variable to "true." Now let's say you have a cat, and you put that cat inside the box. Now that there's something inside the box it would no longer be empty. So you would want to set the value of "isEmpty" to "false" because there is something in the box.

hakan Gülay
hakan Gülay
8,489 Points

Thanks for answering again :) I understood what you all said but I couldn't understand this code.Can you explaing step by step please this :)

public boolean dispense(){
  boolean wasDispensed=false;
  if(!isEmpty()){
    mPezCount--;
    wasDispensed = true;
  }
  return wasDispensed;
}
Brendon Butler
Brendon Butler
4,254 Points
public boolean dispense() { // declare a new method with the return type boolean
  boolean wasDispensed = false; // set the boolean variable "wasDispensed" to the value of "false"
  /* VvVvV THIS CODE BLOCK will be skipped if the dispenser is empty
    * if not, subtract one Pez from the dispenser and set the wasDispensed method to "true"
    * since the Pez Dispenser just dispensed 1 Pez
  */
  if (!isEmpty()) { // check to see if the dispenser is not empty (noted by the exclamation mark)
    mPezCount--; // if the dispenser is not empty, subtract "1" from the mPezCount variable
    wasDispensed = true; // if the dispenser is not empty, set the "wasDispensed" variable to the value of "true"
  } 
  return wasDispensed; // return the value of was dispensed
}

I added some comments to the code, that should hopefully clear some things up.

Quick question... public boolean dispense() { boolean wasDispensed = false; if (!isEmpty()) { mPezCount--; wasDispensed = true; } return wasDispensed; } Does dispense() return true or false? I would like to think it returns true because it is the final statement of the if ,but I'm not sure if its out of scope.

Brendon Butler
Brendon Butler
4,254 Points

Mariam Olupitan

It would return false unless the if statement returns true. If the if statement returns false, then it will skip whatever is inside of the if statement's code blocks (curly braces).

For example, let's say that the dispenser is empty.

public boolean dispense() {
  boolean wasDispensed = false; // This will set the "wasDispensed" variable to false

  if (!isEmpty()) { // since the dispenser is empty in this example, it would skip this check...
    // run the code in here if the dispenser is NOT empty (indicated by the exclamation mark "!")
    mPezCount--; // subtract one from the "mPezCount" variable
    wasDispensed = true; // if the dispenser is NOT empty, set the "wasDispensed" variable to true because there is pez to be dispensed

  } // ... it would skip down to here and keep going from this point
  return wasDispensed;
}
hakan Gülay
hakan Gülay
8,489 Points

Thanks so much Brendon! I got it now :) thank you so much :)

Brendon Butler So in the case that it is not empty the wasDispensed variable will be set true and...then what happens? Will it skip down and return true? I'm confused on how it continues to keep dispensing pez if wasDispensed is initially set to false.

Brendon Butler
Brendon Butler
4,254 Points

Mariam Olupitan Oh sorry, yeah. In the case that the dispenser is not empty, it would run the code inside of the if statement (anything between the curly braces) after all that code has been run, it will escape the code block and run any code that's under the if statement.

But if the dispenser is empty, it will completely skip any code within the code block (curly braces) and continue to run any code under it. I should've cleared that up. Sorry! Happy coding!