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 trialSantiago Serrano
2,754 PointsDo variables declared inside a method get deleted after the method ends executing?
Hi, when Craig made the dispense() method, he declared the variable "boolean wasDispensed = false;" inside of it. That made me think that probably variables get deleted after a method ends executing, since:
- It would be useless to keep storing variables that can't be used later on (since the scope of the method ended, and outside this scope they can't be used).
- The variable would be declared twice when the method is run again if it didn't delete itself after the previous iteration of the method, and this would end up in a compilation error, which didn't happen.
I would love if someone could confirm if my theories are correct, if I'm forgetting something ,or not understanding how things work. Thanks in advance!
(The link for the video in which Craig declares the variable is: https://teamtreehouse.com/library/incrementing-and-decrementing, anyways, I don't think it's necessary to watch it to understand my question).
1 Answer
Steven Parker
231,248 PointsVariables are disposed (the technical term) when they go out of scope. This is a function of the "garbage collection" mechanism.
Santiago Serrano
2,754 PointsSantiago Serrano
2,754 PointsThank you for your answer :D