In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the problems of Java Interface constant storage". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the problems of Java Interface constant storage".
Because fields declared in java interface are automatically modified by static final at compile time, that is, they are declared as constants. So interface is usually the best place to store constants. However, there will be some problems in the practical application of java.
There are two causes of the problem:
The first is that the constants we use are not immutable, but cannot be assigned to variables. For example, we define the constant ∏ = 3.14 at the beginning of a project, but due to the improvement of calculation accuracy, we may redefine ∏ = 3.14159, so the reference to this constant should be changed throughout the project.
Second, java is a dynamic language. Unlike static languages such as C++, java's references to some fields can be done dynamically at run time, and this flexibility is a big advantage of dynamic languages like java. It also makes us in the java project sometimes part of the content changes do not have to recompile the entire project, but only need to compile the changed part of the rerelease can change the whole application.
After talking so much, don't you know what I'm going to say? Okay, let's look at a simple example:
There is an interface An and a class B with the following code:
/ / file A.java public interface A {String name = "bright";} / / file B.java public class B {public static void main (String [] args) {System.out.println ("Class A's name =" + A.name);}}
Easy enough. OK, compile A.java and B.java.
Run, type java B, and the obvious result is as follows:
Class A's name = bright
We now modify the A.java as follows:
/ / file A.java public interface A {String name = "bright sea";}
Rerun B class after compiling A.java, type java B, and note: the result is as follows
Class A's name = bright
Why not "Class A's name = bright sea"? Let's use the decompilation tool javap provided by jdk to decompile B.class. Enter: javap-c B, and the result is as follows:
Compiled from B.java public class B extends java.lang.object {public B (); public static void main (java.lang.String []);} Method B () 0 aload_0 1 invokespecial # 1
Did you notice the code labeled 3? By referencing a field of static final, the compiler has compiled the contents of name in interface An into class B instead of a reference to name in interface A. So the changes to name in class B Department interface A cannot be reflected in class B unless we recompile it. If you do this, the dynamic advantages of java will disappear.
Solution, there are two solutions.
The first way is to stop using constants, declare the required fields in class, and remove the final modifier. However, this method has a certain risk, because it is no longer constant, so it may be modified by other classes while the system is running, which goes against our original intention of setting it as constant, so it is not recommended.
The second method is to declare the constant in class and use the class method to get the value of the constant. To keep the reference to this constant simple, we can use a static method. We modify A.java and B.java as follows:
/ / file A.java public class A {private static final String name = "bright"; public static String getName () {return name;}} / / file B.java public class B {public static void main (String [] args) {System.out.println ("Class A's name =" + A.getName ());}}
Similarly, we compile A.java and B.java. Run class B, type java B, and the obvious result is as follows:
Class A's name = bright
Now let's modify the A.java as follows:
/ file A.java public class A {private static final String name = "bright"; public static String getName () {return name;}}
We re-run B class after compiling A.java again, and type java B: the result is as follows
Class A's name = bright sea
Finally, when we get the result we want, we can decompile B.class again to see the change to class B by typing:
Javap-c B, the results are as follows:
Compiled from B.java public class B extends java.lang.Object {public B (); public static void main (java.lang.String []);} Method B () 0 aload_0 1 invokespecial # 1
Note that the code labeled 10 to 15 lines, class B has become a reference to A class's getName () method, when the value of the constant name changes, we only need to modify and recompile the constants in class A, we can change the reference to this constant throughout the application without compiling the entire project, that is, keeping the dynamic advantage of java and keeping our original intention of using constants, so method two is the best solution.
Thank you for your reading, the above is the content of "what are the problems of Java Interface constant storage". After the study of this article, I believe you have a deeper understanding of the problem of Java Interface constant storage, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.