Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get @getter and @setter?

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?

like image 374
Shapci Avatar asked Mar 03 '16 15:03

Shapci


People also ask

What is @getter and @setter?

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.

What can I use instead of getters and setters?

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.

How do I add getters and setters in spring boot?

After you have created the variables in the class, right click, select Source, select Generate Getters and Setters.


1 Answers

@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

like image 178
Jordi Castilla Avatar answered Sep 18 '22 12:09

Jordi Castilla