-->
--> java Interface is the best place for constant storage? !--Black line separator--> The!--end HEADER area--> The!--start body area--> the!--Sidebar gutter-->!--START toc-->< b> !--Introduction of reference-->!--end toc-->!--start Related content area--> "!--end toc-->"!--Start Related content Ar ea-->!--End Related DW Content area--> The!--end STANDARD SIDEBAR area-->
java/l-java-interface/index.shtml# Author1 ">bright (brightsee@21cn.com)
!--Author name (email address) [title], [Company] 2001 x month
-->
Because The fields declared in the Java interface are automatically added with the static final modifier at compile time, which is declared as a constant. Thus interface is usually the best place to store constants. However, there are some problems in the actual application of Java. The
Problem has two causes, first, that the constants we use are not immutable, but are relative to variables that cannot be assigned a value. For example, we define constant ∏=3.14 at the beginning of a project, and because of the increase in computational precision we may redefine ∏=3.14159, at which point the entire project reference to this constant should be changed. Second, Java is a dynamic language. Unlike static languages such as C + +, Java's reference to some fields can take place dynamically during runtime, a major advantage of dynamic languages such as Java. It also makes it possible to change parts of the Java project without recompiling the entire project, but only by recompiling the part that changes the entire application.
So much, you don't know what I'm going to say? OK, let's look at a simple example:
has a interface A, a class B, and the code is as follows: