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

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! :-)