How constants are defined in java.
我想大声告诉你
我想大声告诉你 2017-06-12 09:19:32
0
2
955
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?

我想大声告诉你
我想大声告诉你

reply all(2)
phpcn_u1582

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...

世界只因有你

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)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template