What exactly is the definition of a constant in a Java project?

Source: Internet
Author: User

Background: Due to the recent takeover of an old system, in the process of looking at code logic, see that there are a lot of magic numbers in the program, so start to separate the constants in the system, and then define in the constant class. It could have been done, but I have two questions:
1, why should the illuminated steady be extracted. 2, to extract how to define, defined in the interface, or class. What's the difference.

1 often bright extract is conducive to code reading, and the next time to do this judgment without handwriting or copy, directly through the constant class can be obtained, can avoid some place is username, and some place is name to judge the same logic meaning the same variable problem. But I don't think the extraction would be good for code performance because the constants are allocated in the illuminated steady pool of memory, so there won't be multiple identical constant strings in memory. In general, the main purpose of extracting constants is to avoid magic numbers and improve code readability.

2) are often defined in final class, preventing inheritance and instantiation of other classes. As for why to define in the illuminated steady class rather than the interface, neither Josh nor Ali explained the reason, and I guess it was a single liability principle, since interfaces were defined to allow other classes to be implemented, and classes that were not inheritable and could not be instantiated externally could be a good guarantee of the singularity of constants. I checked the spring constant again, and I found out he was a class, not a interface.

I'll try to answer it myself first, and then I'll see what StackOverflow and Ali programmers say.

StackOverflow recommended tool classes that are enumerated or not instantiated

Ali documents and constants for instructions:

The methods and properties in the recommended interface class do not add any modifiers (public or not), keep the code concise, and add a valid Javadoc annotation. Try not to define variables in the interface, if you must define variables, it must be related to the interface method, and the entire application of the underlying constants.
Positive example: interface method signature: void F ();
Interface base constants represent: String company = "Alibaba";
Anti-Example: interface method definition: Public abstract void f ();
Note: The JDK8 interface allows a default implementation, and this default method is a value-only implementation for all implementation classes

Force does not allow any magical value (that is, undefined constants) to appear directly in your code.
Anti-Example: String key= "Id#taobao_" +tradeid;
Cache.put (key, value);

"Recommended" do not use a constant class to maintain all constants, should be categorized by constant function, maintained separately. For example, cache-related constants are placed under class: Cacheconsts, and system configuration-related constants are placed under the class: Configconsts class.
Description: All-Inclusive of the constant class, it is necessary to ctrl+f to the modified constants, not conducive to understanding, but also not conducive to maintenance.
The reuse level of "recommended" constants has five layers: shared constants across applications, shared constants within applications, shared constants within a subproject, shared constants within packages, shared constants within a class.
1 shared constants across applications: Placed in a two-party library, usually under the const directory in Client.jar.
2 Application-Shared constants: The const directory placed in the modules of one side library.
Anti-Example: easy to understand variable also to be defined as the application of shared constants, two of the siege in two classes, respectively, defines the variable representing "yes":
Class A: public static final String Yes = "yes";
Class B: public static final String YES = "Y";
A.yes.equals (B.yes), expected to be true, but actually returned to false, resulting in an online problem.
3 The Sub-project internal share constant: that is, in the current sub-project const directory.
4 in-Package shared constants: That is, under the current package under the separate Build const directory.
5 shared constants within the class: private static final definition directly within the class.

"Coercion" avoids the use of a class's object reference to access static variables or static methods of this class, unnecessarily increasing the compiler resolution cost, and accessing it directly with the class name.

Ali as far as possible do not define variables in the interface, if you must define variables, it must be related to the interface method. Don't show magic numbers in the program.

Summary : The constants in a Java project define a final class and have a private constructor method, the demo code is as follows:

public final class Propertiesconstant {private Propertiesconstant () {} public stat
    IC final String client_sig_pc = "PC";

public static final String Client_sig_wap = "WAP"; }
Related Article

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.