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

What does this part of the code do?

PezDispenser dispenser = new PezDispenser("Yoda");

2 Answers

Hi Thomas,

It creates a new PezDispenser object, passing "Yoda" into the constructor. This new object is then stored in a variable named dispenser which is of type PezDispenser

This is how i understood this concept:

PezDispenser dispenser = new PezDispenser ("Yoda");

Imagine [Y]ou're a boss of a company. You're sitting in your office drinking whiskey on the rocks. You were thinking about a new project lately, so you call your best [E]mployee. Here's your dialogue. [Y]: Hey, do you remember how to create PezDispensers? [E]: Yeah, sure, what about it? [Y]: Create one for me, let's call it dispenser. [E]: Allrighty boss! But you know, everytime we create those, we have to write something on it! What should I do with this one? [Y]: Let's make "Yoda" on it. [E]: Cool, I'll be right back after i finish.