Quick Java question about private static final keywords for fields

前端 未结 7 1088
梦如初夏
梦如初夏 2020-12-13 18:48

I\'m declaring a field:

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

First, does the order of private static final

相关标签:
7条回答
  • 2020-12-13 18:51
    1. The order doesn't matter, but you can always play around with it - there's only 6 possibilities to test.

    2. I'm not aware of any convention, though I put the visibility modifier first (public/private/protected) so you can eyeball it and it lines up.

    3. If it's fixed then you can do that, but I always think something is a constant only to discover later (during testing, for example) that I want to pass it in. An argument on the command line or a properties file works for that case, and is a minimum of effort to set up.

    0 讨论(0)
  • 2020-12-13 18:56

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

    0 讨论(0)
  • 2020-12-13 18:59

    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

    0 讨论(0)
  • 2020-12-13 19:01

    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.

    0 讨论(0)
  • 2020-12-13 19:02
    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.

    0 讨论(0)
  • 2020-12-13 19:07

    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.

    0 讨论(0)
提交回复
热议问题