Is Java Interface a constant location?
Since the fields declared in Java Interface automatically add Static Final modifiers, that is, the declaration is constant. Thus Interface is usually the best place to store constants. However, there will be some problems when Java's practical application. There are two causes for problems, first, it is not a constant that we use is not a constant, but it cannot be assigned relative to variables. For example, we define constants π = 3.14 in an engineering, and we may redefine π = 3.14159 due to the improvement of calculation accuracy, and the entire project should be changed. Second, Java is a dynamic language. Unlike the static language such as C , Java's reference to some fields can be done in operation. This flexibility is a major advantage in the dynamic language of Java. That is also made in the Java project, there is no change in the content of some content, and the entire application can be changed only by recompiling the entire project, and only the part of the translation change can change the entire application. Telling so much, don't you know what I am going to say? Ok, let's look at a simple example: there is an interface A, a class b, code as follows: // file a.javapublic interface a {string name = "bright";} // file b.javapublic class b {public static Void main (string [] args) {system.out.println ("class a's name =" a.name);}} is simple enough, good, compile A. Java and B.java.
Run, enter Java B, obvious results as follows: Class a's name = Bright We now modify A.java as follows: // file a.javapublic interface a {string name = "bright sea";} Compile A. Java Re-Run B Class Enter Java B, Note: What is the following class a's name = BRight Why isn't "Class a's name = Bright SEA"? Let's use the anti-compilation tool for JDK JAVAP. Void main (java.lang.string []);} method b () 0 aload_0 1 invokespecial # 1