Where to put constants in the classroom: standards and best practices

When encoding some custom stream reader for the result of the script, where there were quite a few constants in the class (mainly for expected tags and keywords), I was wondering if there were any standards, conventions or best practices for where to put the constants (read here fields static final) inside the class?

In particular, is it considered better that each constant is at the top of the class or groups them in the class area where they are useful, and groups common domains?

Having placed everything at the top, it seems to me that it may be easier to find everything you are looking for in one place, but it can become stunning if this area gets bigger:

public class Test {
    // Constants.
    private static final String CLASSNAME = Test.class.getSimpleName();
    private static final String COMMON = " = ";
    private static final String CONSTRUCTOR = "#constructor";
    private static final String METHOD_1 = "#method1";
    private static final String METHOD_2 = "#method2";

    public Test(String message) {
        System.out.println(CLASSNAME + CONSTRUCTOR + COMMON + message);
        method1(message);
        method2(message);
    }

    private void method1(String message) {
        System.out.println(CLASSNAME + METHOD_1 + COMMON + message);
    }

    private void method2(String message) {
        System.out.println(CLASSNAME + METHOD_2 + COMMON + message);
    }

    public static void main(String[] args) {
        new Test("Hello world!");
    }
}

, , , , , :

public class Test {
    // Common constants.
    private static final String CLASSNAME = Test.class.getSimpleName();
    private static final String COMMON = " = ";

    // Constructor constants.
    private static final String CONSTRUCTOR = "#constructor";

    public Test(String message) {
        System.out.println(CLASSNAME + CONSTRUCTOR + COMMON + message);
        method1(message);
        method2(message);
    }

    // Constant proper to method1(...).
    private static final String METHOD_1 = "#method1";

    private void method1(String message) {
        System.out.println(CLASSNAME + METHOD_1 + COMMON + message);
    }

    // Constant proper to method2(...).
    private static final String METHOD_2 = "#method2";

    private void method2(String message) {
        System.out.println(CLASSNAME + METHOD_2 + COMMON + message);
    }

    public static void main(String[] args) {
        new Test("Hello world!");
    }
}

:

Test#constructor = Hello world!
Test#method1 = Hello world!
Test#method2 = Hello world!

, , , - (un) , , .

, Javadoc , .

+4
2

Oracle. , , .

Oracle 1999 , . Google , :

3.4.2

, . -.

, , , . , , " ", .

, , . , .

+4

. @AlexisLeclerc , , , . , .

, , , .

+1

Source: https://habr.com/ru/post/1531835/


All Articles