0

So i get that private vars start with 's' like

private static int sSomeInt;

but what about those that are also final ?

i like that public static final variable names are in all caps, since it seems to me that i can't modify a variable in all-caps. for me, these two feel too much the same to me, even though they're supposed to be different?

private static int sMutableInt;
private static final int sImmutableInt;

and i can't tell by the style of the variable name itself that i can't re-assign sImmutableInt (only by the actual name of it)

  1. does Android not care about these? (not mentioned in https://source.android.com/source/code-style.html
  2. what is a common practice for this case?
2
  • Are you specifically talking about changes to open source Android code that you may make a contribution for? That is the only place where the "rules" apply (not to an application you may develop that runs on Android). Commented Mar 11, 2014 at 23:15
  • sure, but i'd like to adhere to those suggestions anyways for my own apps/code. i guess that makes me a communist, but i prefer standardization for coding Commented Mar 11, 2014 at 23:22

2 Answers 2

3

The source code generally USES_ALL_CAPS_AND_UNDERSCORES for all static final data members, whether public or private.

Sign up to request clarification or add additional context in comments.

4 Comments

All caps with underscores is for PUBLIC static final <type> constants. So the case in the original question would match the "rule". (sImmutableInt)
hmmm this seems correct, and more intuitive. out of curiosity, how many occasions have you seen this? ( more than 30?)
@DavidT.: More times than I can count. You are welcome to review the Android source code and see this pattern in use, such as in AsyncTask and Activity. To flip it around, I cannot recall ever seeing a static final not using THE_ALL_CAPS_AND_UNDERSCORES style.
@CommonsWare awesome. i just wanted to make sure. yeah i agree, that's what i saw in a lot of android source code also. leaving this answer unaccepted for a little bit longer to see if anyone else has any interesting perspectives that are different (since this question is slightly subjective)
2

That basically depends on the Java code convention you stick to. Android source code and its SDK use the all caps and underscore convention for constants.

I guess that Android is following Google Java Style: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html

About your question:

// Constants
static final int NUMBER = 5;
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
static final Joiner COMMA_JOINER = Joiner.on(',');  // because Joiner is immutable
static final SomeMutableType[] EMPTY_ARRAY = {};
enum SomeEnum { ENUM_CONSTANT }

// Not constants
static String nonFinal = "non-final";
final String nonStatic = "non-static";
static final Set<String> mutableCollection = new HashSet<String>();
static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable);
static final Logger logger = Logger.getLogger(MyClass.getName());
static final String[] nonEmptyArray = {"these", "can", "change"};

http://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s5.2.4-constant-names

For instance, you can check the the View class (http://developer.android.com/reference/android/view/View.html#GONE).

public static final int GONE = ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.