0

Is there any difference between final static int x = 1; and static final int x=1? In other words, will the java compiler represent x in exactly same way in both the cases ?

EDIT: Is there any kind of precedence or priority the compiler has with regard to static and final?

Shan
  • 5,054
  • 12
  • 44
  • 58

5 Answers5

9

There is no difference the compiler will represent them in the same way.

It is just personal preference, personally I use static final and this is what I generally see used by other developers.

cowls
  • 24,013
  • 8
  • 48
  • 78
6

There is no semantic difference. From the JLS (§8.3.1 Field Modifiers):

FieldModifier: one of
     Annotation public protected private
     static final transient volatile

If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.

Thus the preferred form is

static final int x=1;
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

java compiler represents x in exactly same way in both cases

Fr_nkenstien
  • 1,923
  • 7
  • 33
  • 66
1

No difference. I prefer to use final static int

AmitG
  • 10,365
  • 5
  • 31
  • 52
0

This is from the Java Language Specification §8.3.1:

FieldModifier: one of Annotation public protected private static final transient volatile

If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.

So there is no difference between the two in your case. Only that static final is more customary.