11

The convention says: "Non-public, non-static field names start with m. Other fields start with a lower case letter". Does it refer to class field only (like in example 1) or to all field (like in example 2)?

Example 1

public class One {
   private int mFieldOne;
   private int mFieldTwo;

   public void someMethod(){
      int methodFieldOne;
      int methodFieldTwo;
   }
}

Example 2

public class Two {
   private int mFieldOne;
   private int mFieldTwo;

   public void someMethod(){
      int mMethodFieldOne; //see m here
      int mMethodFieldTwo; //see m here
   }
} 
Giulio Piancastelli
  • 13,500
  • 5
  • 38
  • 59
sandalone
  • 38,958
  • 59
  • 205
  • 324
  • 3
    Note that those "fields" in methods as in your example do not compile, the `private` modifier is illegal there. That should already have turned on a light bulb :) – BalusC Feb 08 '12 at 16:29
  • @BalusC Sorry, this was just an example. I did copy&paste without taking too much care. – sandalone Feb 08 '12 at 16:32
  • 3
    Okay. My point was, you can't tune the visibility of local variables nor make them static, so the statement "non-public, non-static" would already make no sense. – BalusC Feb 08 '12 at 16:34
  • @BalusC Hahaha you're absolutely right! – sandalone Feb 08 '12 at 16:35
  • I still wonder why there are some android coding rules when there have always been java coding rules... – Snicolas Feb 08 '12 at 19:53
  • @Snicolas Why not? We use Java, but making SDK specific coding rules makes such apps distinctive. Like, when you see some image title ic_launcher_app, you immediatelly know it's an image for Android app. – sandalone Feb 09 '12 at 09:15
  • @bergnam and you touch the central point; there is absolutely no need to distinguish android code from java code. There is no special way to program for using swing or any other API and android only differs by its API. It would help building pure java core modules in android apps if there would be no specific coding rules just because you use a few android classeS. – Snicolas Feb 09 '12 at 10:26
  • @Snicolas You're right, unless Google has some plans for the future, like making AJava language :), the same way Apple made Objective C. Just a thought... But frankly, I haven't seen any huge differences in Android Code Style, except that they added a few suggestions on how to name new things which came with Android SDK. Can you name what Android Style changed from the original Java? – sandalone Feb 09 '12 at 11:34
  • 1
    @bergmam : conding conventions (to my mind absolutely awful and useless) for naming private fields, static fields. Other ad-hoc conventions that seem ackward include onXXX for event methods, OnXXX for event listener interface names, unique listener registration method instead of the common multiple listener registration (setOnXXXListener instead of addXXXListener/removeXXXListener). Not that much, but poorly enough, not real plus value.. – Snicolas Feb 09 '12 at 13:52
  • 1
    android: http://stackoverflow.com/questions/2092098/why-do-most-variables-in-android-tutorial-start-with-m – Ciro Santilli新疆棉花TRUMP BAN BAD Feb 09 '16 at 17:54

4 Answers4

6

In your second example, mMethodFieldOne and mMethodFieldTwo are not fields, just variables local to someMethod, so the naming convention does not apply.

Giulio Piancastelli
  • 13,500
  • 5
  • 38
  • 59
4

This is a Android contributor guideline, not Java community.

Follow Field Naming Conventions Non-public, non-static field names start with m.

Static field names start with s.

Other fields start with a lower case letter.

Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.

http://source.android.com/source/code-style.html#follow-field-naming-conventions

Anyway.. I think that this isn't necessary and don't agree with the style.

seufagner
  • 1,130
  • 1
  • 16
  • 24
4

It refers to fields only, which are the class members (= m). The others are local variables.

Andreas Dolk
  • 108,221
  • 16
  • 168
  • 253
3

Inside method access modifier (private) are not acceptable, compile time error. ""Non-public, non-static field names start with m" means instance variables, which is first case. Inside method it will just start with small letter.

kosa
  • 63,683
  • 12
  • 118
  • 157