java – What does the "m" mean at the beginning of a variable name?

Question:

I often come across variables in the code, the name of which begins with one letter, which I cannot always associate with the context. For instance:

mCtx
mDBHelper
mDB
mTabHost

After reading the agreement , I did not find information on what the letter m means in these variables.

Answer:

In Java-based development, the use of the m and s prefixes when naming variables is recommended by Google for android developers:

  • The m (member) prefix is ​​used to name non-public non-static class fields (eg mField).

  • The s (static) prefix is ​​used to name static class fields (eg sField).

  • Constants are named in full capital letters separated by an underscore (eg FIELD_CONST).

  • Other class fields and local variables are named without prefixes with a lowercase letter.

This is done in order to visually separate class fields from local variables, which in turn comes from the fact that the naming of variables of the same value in android development is usually written in the same way. That is:

public class SomeClass {
Field mField;

  public SomeClass (Field field) {
      mField = field;
  }
}

This is correct, whether it is convenient and other abstract approvals or condemnations we leave aside – these are the recommendations for developers for the Android platform.
In addition, the entire Android framework is written according to these conventions and often looking at the source code you get used to this style, the other seems to be already erroneous, and reading code that does not adhere to this style becomes uncomfortable (all this applies exclusively to the code of an android application).
Personally, my IDE is configured to automatically set the required prefixes where required.

Well, the Java convention does not give such recommendations – it is proposed to name all variables and class fields with a lowercase letter and without any prefixes, so if you are not an android developer, you should not name the fields with prefixes.

UPDATE

And now the Google Java Style convention has been published , where itsuddenly says literally the following:

Google-style special prefixes and suffixes such as name_, mName, s_name and kName are not used.

It is also said :

Non-constant class fields (static and others) are written in the lowCamelCase style.

PS: some kind of ambush. I've gotten used to these "m" so much now to get out of the habit again or something ..

UPDATE2

Translated from an article by Cédric Beust , the person responsible for prefixing class fields in the Android Application Coding Guidelines, where he explains how this happened.

Scroll to Top