Is Java Interface the best place for constant storage?

Source: Internet
Author: User

 

BecauseThe field declared in java interface is automatically added with the static final modifier during compilation, that is, it is declared as a constant. Therefore, interfaces are usually the best place to store constants. However, some problems may occur in the actual application of java.

There are two causes of the problem. First, the constants we use are not static, but cannot be changed with respect to the variable value. For example, we define the constant limit = 3.14 at the initial stage of a project, and we may redefine Limit = 3.14159 due to the improvement in computing accuracy. At this time, the entire project should change the reference of this constant. Second, java is a dynamic language. Unlike static languages such as c ++, java can dynamically reference some fields during runtime. This flexibility is a major advantage of dynamic languages such as java. In this way, sometimes some content changes in the java project do not need to be re-compiled into the entire project. Instead, you only need to re-publish the changed part of the compilation to change the entire application.

After talking so much, do you still have no idea what I want to say? Okay. Let's look at a simple example:

There is an interface A and A class B. The Code is as follows:

// File A. java
Public interface {
String name = "bright ";
}

// File B. java
Public class B {
Public static void main (String [] args ){
System. out. println ("Class As name =" + A. name );
}
}

It's easy enough. Well, compile A. java and B. java.

Run and enter java B. The result is as follows:


Class As name = bright

Modify A. java as follows:


// File A. java
Public interface {
String name = "bright sea ";
}

Compile A. java and run B class again. Enter java B. Note: The result is as follows:

Class As name = bright

Why not "Class As name = bright sea "? Let's use the decompilation tool javap provided by jdk to decompile B. class. Input: javap-c B. 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
4 return

Method void main (java. lang. String [])
0 getstatic #2
3 ldc #3
5 invokevirtual #4
8 return

Have you noticed the Code marked 3? Because A static final field is referenced, the compiler has compiled the name content in interface A into class B, instead of referencing the name in interface. Therefore, unless we re-compile class B, the name changes in interface A cannot be reflected in class B. If this is done, the dynamic advantages of java will disappear.

There are two solutions.

The first method is to stop using constants, put the required fields into the class for declaration, and remove the final modifier. However, this method has certain risks. Because it is no longer a constant, other classes may modify its value during system operation and cause an error, which violates the original intention of setting it as a constant, therefore, it is not recommended.

Method 2: place the constant in the class and declare it. Use the class method to obtain the value of this constant. To keep reference to this constant simple, we can use a static method. Modify A. java and B. java as follows:

// File A. java
Public class {
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 As name =" + A. getName ());
}
}

Similarly, we compile A. java and B. java. Run class B and enter java B. The result is as follows:

Class As name = bright

Now we modify A. java as follows:


// File A. java
Public class {
Private static final String name = "bright ";
Public static String getName (){
Return name;
}
}

Compile A. java again and run B class again. Enter java B. The result is as follows:

Class As name = bright sea

Finally, we get the expected result. We can decompile B. class again to see the change of class B. input:

Javap-c B. 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
4 return

Method void main (java. lang. String [])
0 getstatic #2
3 new #3
6 dup
7 invokespecial #4
10 ldc #5
12 invokevirtual #6
15 invokestatic #7
18 invokevirtual #6
21 invokevirtual #8
24 invokevirtual #9
27 return

Note that the Code numbered 10 to 15 lines has changed to reference the getName () method of class A in class B, when the value of the constant name changes, we only need to modify the constant in class A and re-compile it. Without compiling the entire project, we can change the reference of this constant for the entire application, that is, it maintains the dynamic advantage of java and the original intention of using constants. Therefore, method 2 is the best solution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.