search

Home  >  Q&A  >  body text

How constants are defined in java.

public class EnumDome {

    public static final String SUCCESS = "Y";

    public static final String FAIL = "N";

    enum State{

        SUCCESS("Y"),
        FAIL("N");

        private final String state;

        State(String state){
            this.state = state;
        }

        public String getState(){
            return state;
        }
    }
}

What are the characteristics of these two ways of defining constants? Which method is more recommended during use?

我想大声告诉你我想大声告诉你2725 days ago957

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-12 09:21:32

    This question, from a grammatical level, has no suspense. Just Baidu the difference between constants and enumerations.

    But from a design perspective, "enum" is better than "static final".

    This may be a bit abstract. Let me give you an example: For example, I defined a public function (more accurately, a public method) called setResponseState

    One is defined like this: setResponseState(String state) { ... }, using string as parameter.

    One is defined like this: setResponseState(EnumDemo.State state) { ... }, using enumerations as parameters.

    For the first definition, the correct calling method is: setResponseState(EnumDemo.SUCCESS);

    But if you call it like this: setResponseState("abcd");, is there no problem with the syntax of the JAVA language?

    The compiler can still pass this point...but it may cause serious errors during runtime and reduce the reliability of the system.

    The biggest problem here is that "the function parameters are not clear enough", which will cause trouble to developers who use this method. He had to go back and check my source code to finally determine what parameters to pass to me...

    And if I encapsulate this function (method) into a set of framework code and do not provide the source code, then it will be more difficult for developers to check back. This will reduce development efficiency.

    The definition method using setResponseState(EnumDemo.State state) { ... } is very clear. Either SUCCESS, FAIL, or null.

    Developers will almost never make mistakes when calling this function (method), and there is no need to check the source code at all.

    So the more rigorous approach is to use enumerations. Of course this is when defining public functions (methods).

    And if setResponseState is a private function (method), then you can be a little more casual and use string parameters. This "lack of rigor" must only be exposed internally.

    To be more impressive, we need to appear very professional externally, but internally, we can lower our standards a little.

    These are two different application scenarios of constants and enumerations. I hope it will be helpful to you...

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-12 09:21:32

    Simply put:
    Only static final is the way to define constants. The latter one is not, but functionally similar. Which one to use depends on your needs

    In depth:
    Post address:

    Introduction to the use of Enum type in Java language
    Introduction to Java enumeration type (enum)

    reply
    0
  • Cancelreply