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 trialjordan Weitz
472 PointsSooo confused by load()
OK so I've perused the Questions section, and yes this question has been answered but I still can't seem to grasp it.
We create a second load() method, but this time we add a parameter so it's actually an independant method. OK so far I get that. Then we use a new variable inside the parameter for the second method. How does that work? Don't we have to declare the variable before it can be used? If not, then why did we declare but not initialize mPezCount up higher in the workbook?
Then somehow we can enter the first method which then activates the second method even though they arent the same method? How does that work? If we activate the first method (load()) it should change mPezCount to whatever integer we have assigned to MAX_PEZ. If we have already changed mPezCount to MAX_PEZ, then why would we even want to add a specific integer to mPezCount( pezAmount)?
Sorry I'm sooooo frustrated because I really want to learn how to code but I just feel like I can't grasp the bigger picture at all. I know what code to add but I feel like I have no idea why I would add it.
Thanks for your time, I really !don'tAppreciateIt
2 Answers
Damon Revelette
14,253 PointsI'll give it a shot. Hopefully I won't add to the confusion.
A parameter is a local variable that is only used within the body of the method it's declared for. When you declare the parameter for a method you're declaring a variable for that method (and only that method) that "catches" and stores any arguments passed to that method, assuming the data types match. In this case, pezAmount is used only for the ''public void load(int pezAmount)'' method and mPezCount is used by all methods in the class. So mPezCount needs to be declared higher up in the class but declaring pezAmount as a parameter is sufficient.
The two methods, ''public void load()'' and ''public void load(int pezAmount)'' are used for different situations. If the dispenser is empty then no argument is passed and the ''public void load()'' method is used. This method then passes MAX_PEZ to the ''public void load(int pezAmount)'' method since the dispenser needs to be completely reloaded in this situation. Then MAX_PEZ is added to mPezCount. The other situation is when there is something in the dispenser and an int argument is passed so that ''public void load(int pezAmount)'' is used so that whatever was passed is added to mPezCount.
This leads to problems, though, since it leaves open the possibility that mPezCount ends up higher than MAX_PEZ. This is dealt with in the subsequent video on exceptions.
Ryan Ruscett
23,309 PointsHola,
Yeah you are not alone. I am trying to think how to help you the best. This is a loaded question with some exceptions. If you post the code, and you write commends next to the lines you don't understand. Just put //? and I will do my best (can't say it wont be loooong) but I will do what I can to help.
Thanks!
jordan Weitz
472 Pointsjordan Weitz
472 Pointspublic void load() { mPezCount = MAX_PEZ; }
public void load(int pezAmount) { mPezCount += pezAmount; }
This is the code btw :)