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 trialjasonswartzbaugh
3,661 PointsI don't understand this line of code: PezDispenser dispenser = new PezDispenser();
Are we creating a new method here? Can someone please explain what each item in this line of code is? I guess PezDispenser is actually referring to the PezDispenser.java object, but I don't understand what is getting assigned to what in this line of code.
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsDoes this make sense:
When assigning to a variable for the first time, you must declare what type of object it will hold;
<object type> <variable name> = <expression resulting in an object>
When assigning to a variable the For the line:
PezDispenser dispenser = new PezDispenser();
"PezDispenser" is the object type that will be assigned to the variable name "dispenser".
The expression "new PezDispenser()" is a call to the class "PezDispenser" to create a new instance of "PezDispenser" and return that instance. This instance will be assigned to the variable dispenser.
jasonswartzbaugh
3,661 Pointsjasonswartzbaugh
3,661 PointsThanks. I sort of get what you are saying on the assignment to the variable "dispenser", but I guess I'm confused about the difference between the object type "PezDispenser" and class "new PezDispenser()".
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsJava requires that all variables have a declared type. Variables are simply references to objects and must have the same type as the objects they refer to. So while this seems it's from the Department of Redundancy Department, you must declare an variable to be of the same type as the newly created object of the same type before assigning a reference of to the new object to that variable.
Like creating a new empty string would be written as:
String bob = new String();
bob
is a reference of type String, that refers to the newly created object returned by thenew String()
I don't know if this is helping, or if I'm saying the same thing with different words.
Antonio Ruiz
3,072 PointsAntonio Ruiz
3,072 PointsI did exactly what Craig did and get this error:
Any idea why?
Alex Garcia
6,374 PointsAlex Garcia
6,374 PointsExact question i had. Thanks!
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsWhen compiling using
javac Example.java
, the filePezDispenser.java
must be present. When found,PezDispenser.java
will automatically be compiled as well. Try creating the filePezDispenser.java
with content: