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 {
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 {
private static final String CLASSNAME = Test.class.getSimpleName();
private static final String COMMON = " = ";
private static final String CONSTRUCTOR = "#constructor";
public Test(String message) {
System.out.println(CLASSNAME + CONSTRUCTOR + COMMON + message);
method1(message);
method2(message);
}
private static final String METHOD_1 = "#method1";
private void method1(String message) {
System.out.println(CLASSNAME + METHOD_1 + COMMON + message);
}
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
Test
Test
, , , - (un) , , .
, Javadoc , .