How to use lombok's @Getter and @Setter

Oct 11, 2018
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; }
    Well with @Getter you won't need basically any of that anymore, All you need to do is just add

    Code (Text):
    @Getter
    private static PLUGINMAIN instance;

    @Override
    public void onEnable() { instance = this; }
    and you are done! Of course you can do this with other things to like strings, ints, etc or even every variable called in your class!

    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;
    }
    After that @Getter above your class all you will need to do now is, Plugin.getInstance().getList() etc..

    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;
    Now all you have to do is call class#setString() and your all set!

    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;
    }
    Examples!

    [​IMG]

    [​IMG]
  • Loading...
  • Loading...