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?

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Shapci
  • 155
  • 2
  • 2
  • 5

1 Answers1

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

dsomnus
  • 1,391
  • 14
  • 21
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Just remmeber that Lombok libraries must be installed in the IDE. For example see: https://projectlombok.org/download.html – borjab Mar 03 '16 at 16:28
  • 1
    You are welcome Jordi. Great improvements to the answer – borjab Mar 03 '16 at 16:49
  • I've installed the IntelliJ plugin but I don't know how get the classes, IntelliJ can't find Getter or Setter class :/ – Shapci Mar 03 '16 at 19:58
  • @Shapci did you followed instructions in the link? What error you get? – Jordi Castilla Mar 03 '16 at 20:25
  • @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" :( – Shapci Mar 03 '16 at 20:36
  • so, you installed the plugin correctly from plugin repository, right? – Jordi Castilla Mar 03 '16 at 21:06
  • 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 ?? – Anupam Haldkar Aug 03 '20 at 16:39