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.
sarwesh gajbhiye2,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
Sigurd Melsom6,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? :)
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:
- PezDispenser dispenser = new PezDispenser("Yoda"), I want a dispenser with Yoda face
- PezDispenser dispenser2 = new PezDispenser("Darth Vader"), I want the 2nd dispenser to be Darth Vader face
- PezDispenser dispenser3 = new PezDispenser("Anakin"), I want the 3rd dispenser to be Anakin face