How can I get @getter and @setter?
Asked Answered
V

1

7

I often see the following annotations in code:

@Getter
@Setter
public int test = 1;

I know I can create getter and setter methods using this annotations. But which classes/library do I need to use these annotations?

Vagarious answered 3/3, 2016 at 15:32 Comment(2)
projectlombok.org/features/GetterSetter.htmlDishonest
I don't know the reason for the downvotes. Seems quite a reasonable non-trivial question. By the way Lombok is great for this BO and DTOs beans.Doublepark
S
17

@Getter and @Setter are Lombok annotations.


Lombok is a framework that generates repetitive code like, equals, hashCode() or getters and setters in annotated classes or attributes, cleaning up the code, making coding much faster and avoiding human errors because of forgetting some parts...

Just note one thing: your attribute is public, what has not much sense when you insert getters and setters:

@Getter
@Setter
private int test = 1;

Is the equivalent to:

private int test = 1;

public int getTest() {
    return test;
}

public void setTest(int test) {
    this.test = test;
}

How to get Lombok into your project:

  • If you use Eclipse / NetBeans download here the jar and add it to your project following the instructions.
  • IntelliJ has it's own Plugin by Michail Plushnikov:
  • Maven

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
  • Other repositories services (Ivi, SBT, Graddle) check here

Strauss answered 3/3, 2016 at 15:41 Comment(7)
Just remmeber that Lombok libraries must be installed in the IDE. For example see: projectlombok.org/download.htmlDoublepark
You are welcome Jordi. Great improvements to the answerDoublepark
I've installed the IntelliJ plugin but I don't know how get the classes, IntelliJ can't find Getter or Setter class :/Vagarious
@Vagarious did you followed instructions in the link? What error you get?Strauss
@JordiCastilla Yes I did. The error is, it doesn't find Getter class. But it says an error when try to compile something: "java: Unrecognized option: -javaagent:lombok.jar" :(Vagarious
so, you installed the plugin correctly from plugin repository, right?Strauss
I am able to install and added dependency as well getters and setter have imported as well but when I am using it says unable to import ??Usherette

© 2022 - 2025 — McMap. All rights reserved.