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 Harnessing the Power of Objects Incrementing and Decrementing

sarwesh gajbhiye
sarwesh gajbhiye
2,578 Points

can someone explan in detail what this line means "PezDispenser dispenser = new PezDispenser("yoda");"

please tell me in detail i am unable to understand anything PezDispenser dispenser = new PezDispenser(); what is dispenser ? in this line what it is variable , object and why what does "new" do and whats the difference in PezDispenser of right and left
and "dispenser.getCharacterName()" this one too

sorry this is my first programming language so all this stuff in new to me and tuff to understand . thanks in advance

3 Answers

Sigurd Melsom
Sigurd Melsom
6,107 Points

Hi, here is what each word in the line means/does, starting from the left:

  • PezDispenser: This is the type of the variable (i.e. This would have been "int" if the variable "dispenser" was storing an integer, but since it's supposed to store an instance of an Pezdispenser object, the type is "PezDispenser")
  • dispenser: This is the name of the variable storing the new Pezdispenser instance.
  • new PezDispenser(): This is where a new instance of PezDispenser is initiated. And since it is on the right side of the equal sign, the new instance is stored in the variable "dispenser".

Did this clear things up? :)

sarwesh gajbhiye
sarwesh gajbhiye
2,578 Points

so PezDispenser is a variable or object ?

Sigurd Melsom
Sigurd Melsom
6,107 Points

PezDispenser is an object, and it is stored in the variable dispenser

Sigurd Melsom
Sigurd Melsom
6,107 Points

PezDispenser is an object, and it is stored in the variable dispenser

Kenrick Morgan
Kenrick Morgan
441 Points

Hello! Could you please also explain what the line;

   return wasDispensed;

is for? Thanks!

As far as I understood, The first PezDispenser is the object itself, it tells you which object you're creating, in your case is a PezDispenser. If you were asked to create for example a house, you would write House instead. The word "dispenser" is just a variable, basically how you want to call it. For example, if you want to make the second PEZ dispenser, the code would look like "PezDispenser dispenser2"

"new" just means that you're creating something new .The last PezDispenser is the class itself, the one from PezDispenser.java. Basically it tells you from where you're getting the information to create a new PezDispenser. Lastly, the brackets are the parameters of the dispenser. So to give some examples:

  1. PezDispenser dispenser = new PezDispenser("Yoda"), I want a dispenser with Yoda face
  2. PezDispenser dispenser2 = new PezDispenser("Darth Vader"), I want the 2nd dispenser to be Darth Vader face
  3. PezDispenser dispenser3 = new PezDispenser("Anakin"), I want the 3rd dispenser to be Anakin face