How to use lombok's @Getter and @Setter
-
For this tutorial you will need to be using a Build Tool like Maven or Gradle and a compatible IDE.
First you will need to install the Lombok plugin for your respective IDE. (Look at the List then click on your IDE and it will redirect you to an install page for that IDE)
After you have installed the Lombok plugin lets add it to your project as a dependency.
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
Now that we have Lombok successfully installed into our project and IDE lets get on with what I will be showing you in this Wiki Page.
@Getter
The @Getter annotation is probably what you will be using the most from Lombok.
A lot of the time in your plugin specifically in the main class you'll have something like,
Code (Text):private static PLUGINMAIN instance;
@Override
public void onEnable() {
instance = this;
}
@Override
public void onDisable() {
instance = null;
}
public static PLUGINMAIN getInstance() { return instance; }
Code (Text):@Getter
private static PLUGINMAIN instance;
@Override
public void onEnable() { instance = this; }
To make a Getter for every variable in that class just add @Getter above the class.
Code (Text):@Getter
public class Plugin extends JavaPlugin {
@Getter
private static Plugin instance;
private List<String> list;
private String string;
private int number;
private Economy econ;
}
Now that I've shown you the functionality of @Getter lets move on with @Setter!
@Setter
@Setter is like @Getter but instead of getting things it sets things! So its basically the polar opposite of @Getter if you think about it.
To use @Setter you use it like you would @Getter basically,
Code (Text):@Setter
private String string;
@Setter
private List<String> list;
You can also add @Setter above your class to make a Setter for all of the variables in the class and you can even do both!
Code (Text):@Getter
@Setter
public class Plugin extends JavaPlugin {
@Getter
private static Plugin instance;
private String string;
private int number;
private List<String> list;
}
- Loading...
- Loading...
XenCarta PRO
© Jason Axelrod from 8WAYRUN.COM