Constant values ​​not reflected at runtime

Suppose you compile the following two classes. The first is intended to represent the client; the second is the library class.

public class Test{ public static void main(String[] args) { System.out.println(Lib.FIRST + " " + Lib.SECOND + " " + Lib.THIRD); } } public class Lib{ private Lib() { }; // Uninstantiable public static final String FIRST = "the"; public static final String SECOND = null; public static final String THIRD = "set"; } 

prints:

{zero set}

Now suppose you modify the library class as follows and recompile it, but not the client program:

 public class Lib{ private Lib() { }; // Uninstantiable public static final String FIRST = "physics"; public static final String SECOND = "chemistry"; public static final String THIRD = "biology"; } 

prints:

{chemistry set}

Why is the SECOND value changed, but not FIRST or THIRD ?

+4
source share
2 answers

To compile your client program, the known constants of the caveat constant, you must also recompile it.

See also:

+7
source

Values ​​(i.e., "and" and "set") are inserted, and null not compiled by the compiler at compile time. To avoid such actions, you can use the access method.

+3
source

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


All Articles