Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating enums with lombok

Tags:

I am using project lombok with my application. I was creating an enum. If I use

@AllArgsConstructor

annotation with my enum, it doesn't recognise the constructor, enum throws and error that it cannot take string argument.

How to resolve this?

import lombok.Getter; import lombok.AllArgsConstructor  @AllArgsConstructor public enum Direction {     NORTH("NORTH"), // all these enums give error, for no constructor     SOUTH("SOUTH"),     EAST("EAST"),     WEST("WEST");      @Getter private String value; } 

P.S.: I am using intellij-idea, which has lombok plugin install. My lombok dependency version is: 1.16.20

like image 656
Priyank Thakkar Avatar asked Apr 15 '18 01:04

Priyank Thakkar


People also ask

Can we use Lombok for enum?

You can define the inner Fields enum/class yourself, in which case lombok will add all the enum constants / public static final fields you haven't written yourself.

Can you use == for enums?

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.

Can enums have setters?

Update: It's possible to have setters in enum types.

Can you assign values to enums?

You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.


2 Answers

You also have to have enabled "Enable annotation processing" in Settings -> Build -> Compiler -> Annotation Processor

like image 154
Martin Tlachač Avatar answered Sep 18 '22 20:09

Martin Tlachač


That’s usually an issue with your IDE, and strangely, it gets fixed after restarting the IDE. It’s when the plugin lombok is not in the effect yet.

like image 37
Henrique Ordine Avatar answered Sep 21 '22 20:09

Henrique Ordine