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 trialRanak Bansal
1,207 PointsWhat is the purpose of 'dispenser.characterName'
Hi,
I understand everything in the video up until he types in dispenser.characterName in the system.out.printf function (6:25 in the video). Is this a method we have previously learned and I have just forgotten, or what?
Thanks
1 Answer
Qui Le
10,998 PointsI just want to offer you my explanation: Since we have the blueprint PezDispenser, aka class, we can create many instances of that class with the keyword new in Java. Inside class PezDispenser, there is data field known as characterName. This data field is associated with each and every instance created from the class PezDispenser. And we can directly access this data field with the dot notation in Java: specifying name of the object followed by a dot (period) followed by the name of data field/property. When Craig creates an instance of class PezDispenser named dispenser, we can retrieve the name of this instance as follows: dispenser.characterName (So this is not a method, but rather a property of a PezDispenser instance that we can access with dot notation).
Soraia Carmo
3,527 PointsSoraia Carmo
3,527 PointsHi Ranak,
If you go back to 4:25, the instructor creates a new file, with a class named PezDispenser. In that class he creates a variable named "characterName".
The reason why he writes "dispenser.characterName" is because he's accessing that variable from the PezDispenser class (which was initialised in Example.java as "dispenser" ==> PezDispenser dispenser = new PezDispenser();)
Hope it helps!