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 trialChad Lewis
1,191 PointsmMemberVariable or this.memberVariable naming convention?
I remember in the earlier Java intro videos that Craig had mentioned that using the "m" or "this" naming convention was correct either way. It was a style choice that I think it came down to. My question is what is generally the current way to name these member variables in your Classes? I'm pretty sure JavaScript uses "this" as its constructor and I just wonder if this would influence anyone new to Java to use this style also? In short, what is the current standard for Java? Thanks! (Sorry for the long question)
1 Answer
Pedro Cabral
33,586 PointsHi Chad,
mVariableName is what's called a member prefix/enconding that is/was used in Android circles. The keyword this on the other hand represents the current object. I don't think those are comparable concepts, i.e: you still can use the word this to point to a class field that starts with the letter m. Craig mentions that as you progress through the courses and even how to adapt using IntelliJ IDE. I think the key thing is to adapt to the existing code base. Meaning if the project you are working on is on Android and it uses the prefix m, you probably should also it use to be in line with the rest of the team. On the other hand if you find yourself in a project for backend using Java, I don't think you will see that kind of variable name. Regarding the word this, that's a totally different subject. The word this is used in constructors when the class field has the same name as the argument in the constructor: then it knows that this refers to the field property rather than the variable inside the constructor's scope.
Chad Lewis
1,191 PointsChad Lewis
1,191 PointsThank you Pedro, very thorough explanation! I think, for me, it will take some time and practice for these terms/concepts to sink in completely, but your answer has given me a clearer understanding. Much appreciated!