Jump to content

(Java) Why declare & use fields in Enum?

Constructors can be declared in enums in order to attach specific values to the enum. This can be done without making a field or using a constructor body, yet people do that anyway.

The only reason I can think of is for using a getter method. Is that the only reason? If not, what is the reason?

 

public enum Wood {
	PINE(true);
	Wood(boolean isHard){}
}

VS

public enum Wood {
	PINE(true);
	private final boolean isHard;
	Wood(boolean isHard) {
		this.isHard = isHard;
	}
}

 

These are probably dumb questions, but any other reasons for why fields would be used in enums (not specifically for use in constructors)?

Java Trainee (Novice)

Thanks in advance~

Link to comment
https://linustechtips.com/topic/1438672-java-why-declare-use-fields-in-enum/
Share on other sites

Link to post
Share on other sites

You can have methods inside an enum, not just the constructor. I've not run into a situation where I felt like that was a good idea, but it is allowed for what it's worth. You could even put the main method inside an enum if you really wanted to. Fields would be useful for that, although that circumstance seems rather niche. I'd be interested in hearing from someone who has found use for putting methods in an enum in an actual project.

Link to post
Share on other sites

Not all things in language are well built. This feature in Java is just making a enum work exactly like a struct. I guess at some point a developer at Sun didn't felt like changing an enum for a struct and change the way enum worked so it works like a struck would without changing the code everywhere else.

Link to post
Share on other sites

public enum Wood {
    PINE(true),
    MAPLE(false),
    ;

    Wood(boolean isHard) {}
}

Adding a constructor parameter that isn't used is the same as a method parameter that isn't used: useless. Any decent IDE and/or plugins like SonarLint will actually tell you to remove it, since it serves no purpose.

image.png.2be6fd1bd7815dea30131a562666db70.png

 

If you found code that does this, then I'd assume the developer who added it either forgot to fully implement it, or it's a leftover from code refactoring. You would typically add a field and a getter for these additional properties. You can use it to associate additional values with enum constants if they are closely related.

Spoiler
public enum Server {
    LTT("https://linustechtips.com"),
    GOOGLE("https://google.com"),
    ;

    private final String url;

    Server(final String url) {
        this.url = url;
    }
    
    public String getUrl() {
        return url;
    }
}

 

16 hours ago, YoungBlade said:

I'd be interested in hearing from someone who has found use for putting methods in an enum in an actual project.

We've got stuff like this in our code:

public enum ExampleType {
    PRIMARY {
        @Override
        public String getUuid(final Example example) {
            return example.getPrimaryUuid();
        }
    },

    SECONDARY {
        @Override
        public String getUuid(final Example example) {
            return example.getSecondaryUuid();
        }
    },
    ;

    public abstract String getUuid(final Example example);
}

I've anonymized it, which makes it a bit more abstract, but I hope the basic idea is clear. Of course you could achieve the same result with a switch case in some utility class.

 

We've also got other cases where specific functionality needs to be executed based on some enum constant. This way the code is directly associated with the enum, instead of moving it into a service or helper class.

 

You can also use it to e.g. implement a state machine, where each state can e.g. tell you what the next state is, or whether it's valid to switch to a certain state going from your current state.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×