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

How did the dispense method run without being told to?

the dispence method didnt got told to dispense in the main class but it did run anyway?

Thanks for the ask ... I've never watched the vid so I'll go through that now and get back to you shortly!

1 Answer

Hi Kenneth,

The dispense method is getting called within Example.java. Even though itis just being used to "check" inside the while loop, the function does get called.

So where your code is:

while(dispenser.dispense){
  // do stuff
}

This is calling the dispense method on the instance of the PezDispenser class, dispenser - if the return value of that method/function is true it'll run the code inside the while loop. If the returned value is false, which means the call to isEmpty() was true then the code inside the while block won't run.

But in either scenario, the method was called so it was told to run - in answer to your question!

I do hope that made sense?!!

Steve.

Thank you steve! can you please answer my other question "confuse of load(MAX_PEZ)"

Done! :-)