What is the var declaration in Lombok?

What is Lombok?

Project Lombok is a Java library that helps reduce boilerplate codeA section of code repeated in multiple places with a little variation.. Java is a very verbose language where we can avoid repetitive code like getters, setters, etc. Lombok reduces the boilerplate code with annotations that get plugged during the build process.

We can easily add Lombok to the project as one of the dependencies.

For a Gradle project, we can add the following two lines to the dependencies section of the build.gradle file:

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

For a Maven project, we can add the following lines to the dependencies section of the pom.xml file:

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>

The var keyword

The @var keyword is used to define local variables without specifying the type. Refer to the following examples:

Vanilla Java Using Lombok
String platform = "name"; val platform = "name";
ArrayList<String> names = new ArrayList<String>(); val names = new ArrayList<String>();

Since the variables declared using var are not final, further assignments of the variables are allowed. But, the new values must fit the appropriate inferred type from the first initialization of the variable.

For example, var platform = "educative";. If we try to assign an integer value to the platform - platform = 5;, the build will fail to indicate incompatible types.

Code

Let’s look at the code below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Explanation

  • Lines 1 to 3: We import the relevant classes.
  • Line 8: We define an ArrayList called platform using the var keyword.
  • Line 9: We add an element to platform.
  • Line 10: We print the contents of platform to the console.
  • Line 11: We assign a new arraylist with different contents to platform.
  • Line 12: We print the contents of platform to the console.

Free Resources