Java Class Fields Naming Style

Bram Yeh
2 min readJan 6, 2018

--

In our android team, we follows the generic android and java coding guide as Google Java Style Guide, AOSP Java Code Style for Contributors, Ribot Android-Guidelines and RAIN Android App‎ Java Styles. And in some place, we have some accustomed style our teams and projects, for example, the class’s field naming is a little different from previous recommended ones.

Class Fields Naming

  • Static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
  • Static mutable field names start with s.
  • Member field names of the non-model class should start with m.

The definition of non-model class is that class has complex algorithm and complicated business logical, e.g. classes of Fragment, Activity, custom view, account manager, network client and etc.

Here we can discuss more but it’s easier to code review on Github if there is m or s for field names. We can easily identify it’s local variable or member variable.

  • Member field names of the model class (like C/C++ structure) shouldn’t start with m.

The definition of model class is that class looks like C++ structure, sometime it has little and simple business logical, but the major usage of this class is to store information and instances. For example, ViewHodler, data model and etc.

I don’t suggest to add prefix m to member field name or not just depending on whether it’s public or non-public field.

  • If this field’s type is an android component, it should add component as postfix, for example, ImageView titleImageView, Button addToCartButton. And it's the same guild for custom view, for example, NoResultView noResultView.
  • Field names start with a lower case letter.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Bram Yeh
Bram Yeh

Written by Bram Yeh

Lead Android & iOS Mobile Engineer at Yahoo (Verizon Media) Taiwan https://www.linkedin.com/in/hanruyeh/

No responses yet

Write a response