简体   繁体   中英

NAME(“name”, true), what does true do here?

public enum Properties
{
    NAME("name", true) ,
    CATEGORY("category",false);

    ...
}

I have a enum like this. And it is used like this:

myMap.put(Properties.NAME, name);

My question is, seems I am using the "name" from NAME ? What does that boolean value do here? "NAME("name", true) ,"

Thanks

Edited:

But my question is when I use it like this "Properties.NAME" I am getting "name" , how can I actually get that "true" ?

The constructor of your enum has two parameters: a String and a boolean. For example:

public enum Properties
{
    NAME("name", true),
    CATEGORY("category", false);

    private final String s;
    private final boolean b;

    private Properties(String s, boolean b) {
        this.s = s;
        this.b = b;
    }

    public String getS() {
        return s;
    }

    public boolean getB() {
        return b;
    }
}

Now, Properties.NAME.getS() returns "name" and Properties.NAME.getB() returns true .

Properties.NAME returns the object NAME of your enum Properties , and when you use it as a String , it will call the method toString() (like all objects in Java). And toString() call name() which returns the name of the object. Here "NAME" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM