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 trialSjoerd Filmer
72 PointsCan anyone explain to me why you change facts to mFacts? what does the m do?
i must have missed that while watching the video. I am not a native speaker so it must have slipped past me when he explained it.
2 Answers
Gloria Dwomoh
13,116 PointsThat means.. "member variable" so we used "m" usually that helps with understanding scopes for example if you have a class called Bakery
public class Bakery {
private String cookie;
// Methods (abilities of the object)
public void setProducts(String cookie) {
cookie = cookie;
}
it can be confusing to know which cookie you are assigning to which. The "cookie" on the right side has scope only inside the setProducts method (and is the variable passed in the method) while the "cookie" in the left side has scope inside the whole class called Bakery (and it's the one in " private String cookie"). So in order to avoid this confusion and differentiate things he added "m" which signifies member variable so that it will be easy to tell which variable you are using. So my example with a member variable changes to...
public class Bakery {
// Member variables (properties about the object)
private String mCookie;
// Methods (abilities of the object)
public void setProducts(String cookie) {
mCookie = cookie;
}
and now you can understand which variable is each and also clearly see its scope.
I hope this example helped.
David Miller
Courses Plus Student 8,268 PointsThis is very controversial practice to prepend member variables with "m". I'd avoid this especially in the current case where it's not needed at all.