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
Joseph Nickalo
432 PointsUsing "methods"
Hello, I am just wondering why we need to use the get method after defining the mInsertObjectHere (for the Pez thing).In the first block in the first part of the video, where we defined class, we also defined the mObject or rather the name (yoda) of the individual pez dispenser. why do we have to delete Yoda from the first block then write the same thing in another block, when we can leave yoda at the top. Seems like adding extra work to me, but i dont know lol
1 Answer
Allan Clark
10,810 PointsIt comes down to the structure and function of the PezDispenser Class. In Object-Oriented Programming (OOP) we create data structures call Objects that can model and behave like real-world Objects. When we are writing a Class we are defining the general information about the Object, its State (class/member variables) and its Behavior(methods).
We do not want to define the value of the member variable at the top because for most member variables they will be different for each instance of the Object. The Yoda PezDispenser is an instance of the PezDispenser Class. If we defined the mName at the top, ALL PezDispenser Objects would be Yoda. So we just declare the variable at the top and wait till the PezDispenser Object is created to set what the mName variable will be.
We only want to put actual values in the variables at the top if it is supposed to be constant through all the Objects created. For example we may want to define the MAX_COUNT constant to denote the number of pez that can be loaded at one time, which will be the same for all PezDispensers.
I hope this helps, please let me know if there is anything I can clear up.
Joseph Nickalo
432 PointsJoseph Nickalo
432 PointsThis answered all my questions, then some I didnยดt even know I had. Thanks and good night