Home > Net >  Lombok not generating getter and setter?
Lombok not generating getter and setter?

Time:12-16

First time using lombok, and if I'm understanding it correctly, lomboks getter and setter annotations generates setter and getter methods without having to code it, but for some reason I'm getting an "undefined method" error whenever I try to call a getter/setter method.

What's wrong here?

import lombok.Getter;
import lombok.Setter;

public class Student {
    @Getter @Setter
    private String firstName;
    @Getter @Setter
    private String lastName;
    
    public Student(String firstName, String lastName) {
        super();
        this.setFirstName(firstName); // method undefined error here
        this.setLastName(lastName);   // method undefined error here
    }
}

CodePudding user response:

Your code sould work as this, Did you install lombok's plugin on your EDI ?

CodePudding user response:

Lombok was not installed in my IDE.

Installing it fixed the problem.

CodePudding user response:

Another easy way is to use the @Data annotation, which generates getters and setters for every property in your class.

import lombok.Data;

@Data
public class Student {
    private String firstName;
    private String lastName;
    
    public Student(String firstName, String lastName) {
        super();
        this.setFirstName(firstName);
        this.setLastName(lastName);
    }
}

Quick note: importing something doesn't use it. It simply tells the program something may be used. If you are using a modern Java IDE (VSCode or IntelliJ), classes, annotations, etc. will be automatically imported as you use them.

CodePudding user response:

try like this

@Getter
 @Setter
public class Student {
   
    private String firstName;
    
    private String lastName;
    
    public Student(String firstName, String lastName) {
    
        this.setFirstName(firstName); 
        this.setLastName(lastName);  
    }
}
  • Related