0

I have read a lot of Android code that looks like this, so many times:

public class MainActivity extends AppCompatActivity {

    EditText editText;
    TextView textView;
    int someInt;
    String someString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //init views
    }

}

I'm still kinda noob in Android programming, but as a classic Java programmer I don't understand why you would not encapsulate your fields as private and make them accessible via getters an setters if necessary. Is there any reason for this? Like some other class or activity in the same package needed direct access to the fields? It may be a silly question, but I couldn't find an explanation for this.

Farbod Salamat-Zadeh
  • 18,039
  • 16
  • 66
  • 118
FerDensetsu
  • 558
  • 3
  • 18
  • Personally I prefer the default access, mainly for laziness. But it is justifiable. It reduces clutter IMO. It is not that dangerous - abuse is unlikely among classes in the same package. It can be beneficial to be accessible from test code. – ZhongYu Jan 25 '17 at 19:57

2 Answers2

3

You should be using private access modifiers for fields that should only be accessible within that class.

So in your case, you are right in saying that the EditText should be a private field, for example.

I'm not sure where you read this code, but to my knowledge, the best practice is to reduce visibility as much as possible.

Farbod Salamat-Zadeh
  • 18,039
  • 16
  • 66
  • 118
  • I have read code like that in Stack Overflow, Github and tutorial websites. Here's an example taken from The Big Nerd Ranch, one of the best known Android books [link](https://github.com/tkunstek/android-big-nerd-ranch/blob/master/20_CameraImage_CriminalIntent/src/com/bignerdranch/android/criminalintent/CrimeFragment.java) and this one is also guilty of using the "m" prefix on fields, which I don't understand either. – FerDensetsu Jan 25 '17 at 18:57
  • @FerDensetsu Hmm - okay. The best practice is to reduce visibility as much as possible, so they haven't done that by using package-private as opposed to private fields. Now, the `m` prefix is an Android coding style convention for private member variables. [This answer](http://stackoverflow.com/a/7072899/4230345) gives a good description of it. A lot of people have differing opinions on the notation (of using 'm'), for example, [this answer](http://softwareengineering.stackexchange.com/a/210730/192774). [Jake Wharton](http://jakewharton.com/just-say-no-to-hungarian-notation/) opposes this too. – Farbod Salamat-Zadeh Jan 25 '17 at 19:03
  • @FerDensetsu Even Google isn't entirely consistent with it themselves (see [here](https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/about/AboutActivity.java#L40), but it is used mostly in the Android Open Source Project, for Android source code. I think it's up to you whether you want to use the prefix convention, but you should definitely use the practice of reducing visibility on fields and methods. – Farbod Salamat-Zadeh Jan 25 '17 at 19:06
  • Yes, I've read about the hungarian notation and I personally dislike it, I understand it's just a coding style and it's up to you wether you use it or not, but keeping class fields as default-access also seems like a pretty extended practice among Android devs, and I'm really curious about the reason or the origin of this practice. – FerDensetsu Jan 25 '17 at 19:15
  • @FerDensetsu I'm not entirely sure why to be honest - if there's a reason for the practice. When I first started programming in Java (for Android), I didn't know the exact difference between visibility modifiers, and perhaps out of laziness, I used default access. However, I doubt the links you showed are from those with little experience, so perhaps they omitted the visibility modifiers for simplicity, or something like that... – Farbod Salamat-Zadeh Jan 25 '17 at 21:04
0

m is used for ease of understanding or differentiation between local and member variable. Also, if you try to access and modify the text of the EditText from another activity without extending it, you won't be able to do it. It's bound to onCreate().

Anurag Singh
  • 5,580
  • 2
  • 24
  • 43