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 trialNathan Quirk
3,679 PointsmVariable notation vs. this.variable notation
In an earlier course, Craig uses the this. notation to indicate the difference between a private variable and one from outside the class. Which is better? I got used to using the this. notation as it's also used in my Java textbook for school. Any reason? What do you guys like?
2 Answers
andren
28,558 PointsNeither is better from a technical perceptive, they are just different coding styles. In the end the code will perform exactly the same.
Using the m
prefix for member variables is pretty common in Android development which is one of the largest areas where Java is used these days. But there is nothing stopping you from using the this
prefix in Android either.
Generally a company will have a coding style guideline that specifics how member variables should be named. So if you join some programming team at a company it will likely be out of your control what style you use, but for hobbyist work you can go with whatever style you prefer.
Lars Reimann
11,816 PointsI usually go without prefixes. IDEs can easily differentiate between local variables and fields and mark them with colors/underlines etc. It also helps with autocompletion. Typing the first characters does not narrow down the search space if everything has the same prefix. But use whatever style you like.