Widziałem niektóre kody w slf4j, jak pokazano poniżej. Nie wiem, dlaczego unikać ciągłego składania tutaj. Czy to konieczne? lub po prostu najlepsza praktyka. jaka jest korzyść z robienia tego?dlaczego unikać ciągłego składania w Javie? Gdy?
Dzięki.
/**
* Declare the version of the SLF4J API this implementation is compiled against.
* The value of this field is usually modified with each release.
*/
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.6"; // !final**
Przeglądałeś wiki na ten temat? Myślę, że wyjaśnia to całkiem dobrze. http://en.wikipedia.org/wiki/Constant_folding – CoolBeans
Możesz użyć metody zamiast eksponować to pole (prawdopodobnie najlepiej), użyj 'final', ale podaj wartość za pomocą metody (' "1.6" .toString() ') lub dodaj jakiś kod, aby nie był on * stałą czasu kompilacji *, ale nie dodaje niepotrzebnego kodu bajtowego ('null! = null?" ":" 1.6 "'). –
Tak, publiczne statyczne stałe są icky! –