Is the Java Interface the best place to store constants?

Source: Internet
Author: User
Tags constant
-->
--> 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:

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.