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?
Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.
You may use lombok - to manually avoid getter and setter method. But it create by itself. The using of lombok significantly reduces a lot number of code.
After you have created the variables in the class, right click, select Source, select Generate Getters and Setters.
@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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With