One, define constants in the interface:
Because the fields declared in Java interface are automatically added to the static final modifier at compile time, they are declared as constants.
Public interface constants{String name = "111"; } public class read{public static void Main (string[] args) {System.out.println ("class Constants ' s name =" + Constants.name); } }
Because a static final field is referenced, the compiler has compiled the contents of name interface constants into class read instead of referencing it. So only the changes in the name of class Read,interface constants are recompiled to be reflected in class read.
II. definitions in a class
public class constants{private static final String name = "111"; public static String GetName () {return name; }} public class read{public static void Main (string[] args) {System.out.println ("class Constants ' s name = "+ Constants.getname ()); } }
Class Read is a reference to the GetName () method of the Constants class, and when the value of the constant name changes, we only need to modify and recompile the constants in class constants without compiling class read.
We can use the Anti-compilation tool provided by the JDK Javap decompile Read.class to see exactly, input: Javap-c read can see the difference between the two ways.
This article is from the Java My Favorites blog, so be sure to keep this source http://lindianli.blog.51cto.com/7129432/1703799
Definitions of Java constants