“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

Have a great weekend, everyone. Last night, two things swept the screen this morning. One is that it snowed in many parts of Beijing (north of Beijing), the first snow; Another was EDG winning the LEAGUE of Legends S11 finals. Congratulations to EDG, a good year with snow.

I believe many friends in the development, often encounter some parameter definition, this kind of parameter is basically fixed, if there is no design database to configure, so many are written in the project, this section and we talk about the constant definition in the project often used.

constant

A constant is a parameter whose value remains constant throughout the run of our project.

Class to define constants

Constants are different from variables, which cannot be modified during project execution. Variables can be evaluated during program execution, so constants use static and final modifications. In the Java development language, the final keyword is used to define a constant that has three types: static, member, and local. The following figure is a static constant defined in the general syntax format

public classThe name of the class{
        public staticStatic constant name of final type = static constant value; }Copy the code

A simple example of defining a static constant from a class is as follows:

public class Constants {
    /** * rare earth */
    public static final String  XITU  = "Rare earth";

    /** ** */
    public static final String  JUEJIN  = "Nuggets";

    /** * more than */
    public static final String  DAIMABUZHI  = "More code";

    /** ** ** /
    public static final String  JUEJINBUTING  = "The nuggets don't stop.";

    /** * nuggets */
    public static final String  JUEJIN_URL  = "https://juejin.cn/";

Copy the code

Use method is very simple, in the project, directly write class name + static constant name, as shown in the following example:

  public static void main(String[] args) {
            System.out.println(Constants.XITU+Constants.JUEJIN+Constants.DAIMABUZHI+Constants.JUEJINBUTING+Constants.JUEJIN_URL);
    }
Copy the code

The output is as follows:

Of course, just a simple example, easy to understand the use of other advanced are basically based on this form of evolution.

Interface definition constant

Interface (refers to the reference type that defines the protocol, which is an introduction given by Baidu Encyclopedia. One class of objects defined in our code using the interface keyword is the interface. Interfaces can be used to implement. Projects often use interfaces to set constant values. The basic format is:

Public interface Name of the interface object {/** * Constant name */The name of a type constant = constant value; }Copy the code

The following code is an example.

public interface InterfaceinConstants {

    /** * JUEJIN_QUEUE */
    String JUEJIN_QUEUE = "JUEJIN_KEY";

    /** * JUEJIN_KEY */
    String JUEJIN_KEY = "JUEJIN_KEY";

    /** * JUEJIN_EXCHANGE */
    String JUEJIN_EXCHANGE = "JUEJIN_EXCHANGE";
}

Copy the code

The use method is similar to the first way, directly call.

 public static void main(String[] args){ System.out.println(Constants.XITU+Constants.JUEJIN+Constants.DAIMABUZHI+Constants.JUEJINBUTING+Constants.JUEJIN_URL); System.out.println(InterfaceinConstants.JUEJIN_EXCHANGE+InterfaceinConstants.JUEJIN_KEY+InterfaceinConstants.JUEJIN_QUEU E); }Copy the code

The following output is displayed:

Enumeration defines constants

The types that are qualified by the enum key in our program are enumerated types. Enumeration types can be managed and used uniformly by a limited number of all constants. Enumeration can be regarded as ordinary classes except that it cannot be inherited. It has its own interface, ordinary methods, static methods, abstract methods, constructors, etc., and can be used as ordinary classes.

General enumerations define constants in the following format:

Public enum Enumeration name {Attribute name (value)1And the value2And the value3.. The value of N); }Copy the code

Here is a simple example value

public enum PushStateEnum {
    PUSH_NO(0."Not pushed"),
    PUSH_FAIL(1."Failure"),
    PUSH_YES(2."Success"); private Integer code; @JsonValue privateString desc;

    public static String getDescByValue(int value){
        for (PushStateEnum item : PushStateEnum.values()) {
            if(item.getCode()==value){
                returnitem.getDesc(); }}return null ;
    }

    public static PushStateEnum getEnum(int value){
        for (PushStateEnum item : PushStateEnum.values()) {
            if(item.getCode()==value){
                returnitem ; }}return null; }}Copy the code

It is used in a similar way to the previous examples, simply printing it.

 public static void main(String[] args) {
        System.out.println(PushStateEnum.PUSH_NO.getDesc());
        System.out.println(PushStateEnum.PUSH_NO.getCode());
        System.out.println(PushStateEnum.PUSH_FAIL.getDesc());
        System.out.println(PushStateEnum.PUSH_FAIL.getCode());
        System.out.println(PushStateEnum.PUSH_YES.getDesc());
        System.out.println(PushStateEnum.PUSH_YES.getCode());
    }
Copy the code

The running results are as follows:

The advantage of using enumerations to define static constants is that multiple constant values can be defined for the same property, which is not the case above.

conclusion

Ok, so these are the ways in which static constants are often defined in projects. You can choose the right way to define constants according to your needs. The scenarios defined by enumeration are mainly used for status codes, type codes, and error codes.

Thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.