Quick Java question about private static final keywords for fields

43,142

Solution 1

I use Checkstyle with Eclipse, which results in a warning if the declaration is in a different order to the one you've specified, citing the Java Language Specification (JLS). For example,

private final static String filename = "filename.txt";

results in

'static' modifier out of order with the JLS suggestions.

They have this page which lists the order they expect, though following the links on that page through to the JLS I can't see anything to back up their assertion of a suggested order.

Having said that, the order they suggest seems to correspond to the order in most of the code I've seen, so it seems as good a convention as any to adopt.

Solution 2

  1. No. But that is the sequence I usually see used.

  2. It's a reasonable choice, but some would prefer a configuration file, either Properties or another file format (e.g. XML). That way, you can change the filename without recompiling.

Solution 3

It's common in Java to give constants (static final values) an all-uppercase name, so I would write:

private static final String FILENAME = "filename.txt";

See also Code Conventions for the Java Programming Language. (Those are Sun's code conventions that the majority of Java programmers use).

Solution 4

The most accepted order of these keywords is private static final. Also you can remember the order of these keywords using PSF pattern that:

P => private / public / protected
S => static / abstract / ...
F => final

Solution 5

see: http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.3.1

8.3.1 Field Modifiers

FieldModifiers:
  FieldModifier
  FieldModifiers FieldModifier

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.

Share:
43,142
Spencer
Author by

Spencer

Updated on September 28, 2020

Comments

  • Spencer
    Spencer over 3 years

    I'm declaring a field:

    private static final String filename = "filename.txt";
    

    First, does the order of private static final matter? If not, is there a standard accepted sequence or convention?

    Second, the filename in my application is fixed. Is this the best was to store its value?

  • Brett
    Brett almost 14 years
    How would you get the file name of the configuration file? :)
  • Matthew Flaschen
    Matthew Flaschen almost 14 years
    @Tom, that's a valid point. Of course, real applications will have many such constants, all of which can be in a single properties file.
  • Jim L.
    Jim L. over 9 years
    That link no longer goes anywhere useful. :-(
  • Jesper
    Jesper over 9 years
    @JimL. fixed the link, although Oracle's page says that this document is "for archive purposes only".
  • serv-inc
    serv-inc about 6 years
    See below for the docs and a link to them.
  • truthadjustr
    truthadjustr over 4 years
    PSF mnemonic is the best! Thanks
  • J.Luan
    J.Luan over 2 years
    Yeah should be private static final String