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 trialMichael Greig
Front End Web Development Techdegree Student 10,328 PointsCan't get my head around dispenser.charactername
I have a feeling this might have been covered in an earlier lesson but please can someone walk me through dispenser.charactername again?
What does the dot do in this case? If we are essentially just pulling through a string here, why does the object 'dispenser' need to be there?
Any help would be much appreciated!
Michael
4 Answers
andren
28,558 PointsThe dispenser
needs to be there because the string in question belongs to it. The dot is used to access public properties and methods that belong to an object. The characterName
string variable is one that exists within the dispenser
object.
The dispenser
object is an instance of the PezDispenser
class, that means that it contains the methods and variables defined within that class. And it is within PezDispenser
that you originally define the characterName
string.
It's also worth noting that individual objects have their own independent state. What I mean by that is that if you created a second object from the PezDispenser
class called dispenser2
in the same way you did with dispenser
, and then changed the characterName
string within dispenser2
. It would not affect the variable within the dispenser
object. They contain independent copies of the variables that were defined in the class.
This explanation is a bit simplified, but might still be a bit confusing as classes and object instances are one of the more complex topics you'll encounter early on in programming. If you have any further questions I would be more than happy to answer them.
Michael Greig
Front End Web Development Techdegree Student 10,328 PointsThat helps hugely. Thanks so much. Having the sentence "The dispenser needs to be there because the string in question belongs to it" in my head has really helped me out.
This is a tricky subject so I might have to take you up on that kind offer!
andren
28,558 PointsNo problem, this is indeed a tricky subject. It's one of the most common stumbling blocks for people starting out with Java and other languages centered around OOP (Object Oriented Programming). But it is a very important topic, it's a feature that the language is fundamentally built around. On the plus side since it is such a fundamental feature you'll get to see plenty of examples of it in use thought the Java course.
I also had a hard time wrapping my mind around it at first, but after seeing it in use enough times and experimenting a bit myself It eventually just "clicked". As cliched as it sounds it's very much one of the situations where practice makes perfect. So if you code along with the various courses and play around with it a bit on your own you will likely gain a decent understanding soon enough.
But as I said if you are ever stuck or just have a general programming question (Java or otherwise) then feel free to post it as a reply to this post, and I'll try to answer any question I can.
Michael Greig
Front End Web Development Techdegree Student 10,328 PointsThat's really kind of you andren thanks so much! I may well take you up on that offer!
I'm really enjoying it so far so fingers crossed I can keep the momentum going. I think your point around the importance of practising outside of the courses is well-made. I'll get on that!
Kurt Daisley
4,228 PointsWanted to add my thanks to Michael for posting the question, and to Andren for answering it so carefully. I had to go over this video a couple times to get my head wrapped around it.