Search code examples
javastaticprivatefinal

Quick Java question about private static final keywords for fields


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?


Solution

  • 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.