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 trial

Java Java Data Structures Efficiency! Building the Model

Nathan Quirk
Nathan Quirk
3,679 Points

mVariable 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

28,558 Points

Neither 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.

I 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.