Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constructor SomeClass() is already defined in class SomeClass

I just upgraded my Spring Boot 1.5.13 application (with Lombok) to Spring Boot 1.5.14 but it now fails to compile with the following types of errors:

constructor SomeClass() is already defined in class SomeClass

for each of my POJOs, e.g.,

@Data
@NoArgsConstructor
public class SomeClass {
    private String someProperty;
}
like image 493
Jan Nielsen Avatar asked Jul 04 '18 15:07

Jan Nielsen


People also ask

How do you call a constructor from another class?

Constructor Calling form another Constructor The calling of the constructor can be done in two ways: By using this() keyword: It is used when we want to call the current class constructor within the same class. By using super() keyword: It is used when we want to call the superclass constructor from the base class.

Can you call a constructor of a class inside another constructor?

Constructor chaining can be done in two ways: Within same class: It can be done using this() keyword for constructors in the same class. From base class: by using super() keyword to call the constructor from the base class.

How do you call a constructor?

No, you cannot call a constructor from a method. The only place from which you can invoke constructors using “this()” or, “super()” is the first line of another constructor.


2 Answers

This is a bug in Lombok 1.6.22; upgrade Lombok to 1.18.0:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.0</version>
    <scope>provided</scope>
</dependency>

or, as a work-around, change the order of the annotations:

@NoArgsConstructor
@Data
public class SomeClass {
    private String someProperty;
}

Details:

The root cause of this bug is a bug in Lombok 1.16.22. Spring Boot 1.5.13 uses Lombok 1.16.20 which does not have this bug, but Spring Boot 1.5.14 updated the Lombok dependency to 1.16.22 -- unfortunately, the Lombok project does not comply with SEMVER which then triggered this bug.

like image 194
Jan Nielsen Avatar answered Oct 01 '22 03:10

Jan Nielsen


I was facing this issue even on the most recent version i.e.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.6</version>
    <scope>provided</scope>
</dependency>

After marking my member variables as final everything worked.

@RequiredArgsConstructor generates a constructor with the required arguments, where a required arguments are final fields and fields annotated with @NonNull (more on that later)

like image 40
Abhishek Avatar answered Oct 01 '22 04:10

Abhishek