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 Harnessing the Power of Objects Filling the Dispenser

Kenneth Emmanuel Kouadio Ziguy
PLUS
Kenneth Emmanuel Kouadio Ziguy
Courses Plus Student 1,125 Points

Still a bit confused about how objects and class work in code!

why should we call the methods in our dispenser class like dispenser.fill() and why not call them like fill() in our example class

samiff
samiff
31,206 Points

You might have asked the question backwards, but the fill() method in the PezDispenser class can't be called on its own from the Example.java file. You need to instantiate (create) an object of the PezDispenser class first before calling the fill() method. If that doesn't clear things up, please try phrasing your question differently so we can try to help.

Hello;

Let say you have a car, a motorcycle and a boat. You have all of them parked next to each out in front of the house. All three fancy items are high tech stuff and they respond to voice commands.

You get up in the morning, walk out of the door. Standing in front of all three of them and you say "start()"

Baam ... all of them started at once :-) and along the way, you really messed up that fancy boat motor :-( striking the concrete driveway.

long story ... but my goal is to illustrate what dispenser.fill() is doing. You are calling on a specific dispenser ... out of a potentially 100s of dispenser that you might have instantiated, and you are telling this particular dispenser to take an action (the fill() action). So if you have several dispenser objects ... dispenser, hisDispenser, herDispenser, theirDispenser, someDispenser, someMoseDispenser.

only dispenser will receive the the fill() command and acts on it.

back to the fancy car, motorcycle and boat parking in front of the house .... example ... if you have said ... car.start() then only the car would have started and you would have saved the boat :-)

Hope this helps.

1 Answer

Kenneth Emmanuel Kouadio Ziguy
PLUS
Kenneth Emmanuel Kouadio Ziguy
Courses Plus Student 1,125 Points

Thank you very much for the answer , now I think is very clear . It is all about choosing specifically what object you want out of many more and the task you want that specific object to do.