Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

Project Lombok is a popular library for reducing Java boilerplate.

In this quick tutorial, we'll take a look at how Lombok's @Getter annotation works on boolean fields to remove the need to create its corresponding getter methods.

2. Maven Dependency

Let's start by adding Project Lombok to our pom.xml:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>

3. Using @Getter on a boolean Field

Let's say that we want Lombok to generate an accessor method for our private boolean field.

We can annotate that field with @Getter:

@Getter
private boolean running;

And Lombok will use its annotation processor to generate an isRunning() method in the class.

And now, we can refer to it, even though we haven't written the method ourselves:

@Test
public void whenBasicBooleanField_thenMethodNamePrefixedWithIsFollowedByFieldName() {
    LombokExamples lombokExamples = new LombokExamples();
    assertFalse(lombokExamples.isRunning());
}

3.1. A boolean Field Having the Same Name as Its Accessor

Let's add another line of code to make the example a little bit tricky:

@Getter
private boolean isRunning = true;

It'd be a bit cumbersome if Lombok created a method called isIsRunning.

Instead, Lombok creates isRunning like before:

@Test
public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() {
    LombokExamples lombokExamples = new LombokExamples();
    assertTrue(lombokExamples.isRunning());
}

3.2. Two boolean Fields With the Same Accessor Name

Sometimes, there can be conflicts.

Let's say that we need to have the following lines in the same class:

    @Getter
    public boolean running = true;

    @Getter
    public boolean isRunning = false;

There are many reasons we should avoid a confusing naming convention like this. One of them is that it creates a conflict for Lombok.

Using Lombok's convention, these two fields would have the same accessor method name: isRunning. But having two methods with the same name in the same class will create a compiler error.

Lombok solves this by creating only one accessor method and, in this case, pointing it at running, based on field declaration order:

@Test
public void whenTwoBooleanFieldsCauseNamingConflict_thenLombokMapsToFirstDeclaredField() {
    LombokExamples lombokExamples = new LombokExamples();
    assertTrue(lombokExamples.isRunning() == lombokExamples.running);
    assertFalse(lombokExamples.isRunning() == lombokExamples.isRunning);
}

4. Using @Getter on a Boolean Field

Now, Lombok treats the Boolean type just a bit differently.

Let's try our same running example one last time, but with Boolean instead of the primitive type:

@Getter
private Boolean running;

Instead of creating isRunning, Lombok will generate getRunning:

@Test
public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() {
    LombokExamples lombokExamples = new LombokExamples();
    assertTrue(lombokExamples.getRunning());
}

5. Conclusion

In this article, we explored how to use Lombok's @Getter annotation for boolean primitives and Boolean objects.

And make sure to check out the samples over on Github.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!