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

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

Calling methods beginning with the lowercase 'm'.

I'm just a little confused on why I would begin a String, boolean, etc. with the lowercase 'm' character. I know that it was sated previously, but I cannot remember the reason that we do this. I might understand better on when to actually use them if I remembered why I do in the first place. Any help will be greatly appreciated and Happy coding.

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Lee,

This is purely a style choice. Most Android development prefixes it's fields (or *m*ember variables) with an m so that you can tell the difference between local variables and those belonging to the instance. Other styles do not. If you do not use a prefix, you must use the keyword this in your methods. So to take the example further:

Using m prefix:

public class Test {
     private int mTestScore = 0;

    public Test(int testScore) {
        // There is a clear difference in the naming
        mTestScore = testScore;
    }
}

Not using the prefix

public class Test {
     private int testScore = 0;

     public Test(int testScore) {
           // Because they have the same name, we must use 
          // the this keyword to tell the difference
           this.testScore = testScore;
     }
}

It's best to adhere to whatever style is going on in the codebase you are working in. I figured that the m prefix naming style would help everyone get used to the Android development standard, as well as practice working in other styles. When we get to Spring we will use the this style as that is the preferred style in Spring development.

Hope that helps!

Ricky Catron
Ricky Catron
13,023 Points

The "m" is used when a variable is a "member" of a class. It belongs to that class. Example:

class Test(){
    mTestScore = 0
}

Goodluck! --Ricky