Is Java Interface a constant location?

xiaoxiao2021-03-06  80

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 4 returnMethod void main (java.lang.string []) 0 getStatic # 2 3 LDC # 3 5 Invokevirtual # 4 8 RETURN Note the code of the label 3? Since a Static Final field is referenced, the compiler has compiled the contents of the Name in Interface A into the Class B instead of reference to Name in Interface A. Therefore, unless we recompiled Class B, the change in Name in Interface A cannot be reflected in Class B. If you do this, the dynamic advantage of Java disappears. Solutions, there are two solutions. The first method is no longer used constant, put the desired field in the Class declaration and remove the final modifier. However, this method has a certain risk, because it is no longer a constant to modify its value by other classes when the system is running, it is contrary to our original intention to set it for constants, so it is not recommended. The second method, puts the constant into the Class declaration, using the Class method to obtain the value of this constant. In order to maintain the simplicity of this constant reference, we can use a static method. We will modify A. Java and B.java as follows: // file a.javapublic class a {private static final string name = "bright"; public static string getName () {return name;}} // file b.javapublic Class B {public static void main (string [] args) {system.out.println ("class a's name =" a.getname ());}} also compiles A. Java and B.java.

转载请注明原文地址:https://www.9cbs.com/read-95659.html

New Post(0)