DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Quick Coding Tip: Why Wait for JDK 10?

Quick Coding Tip: Why Wait for JDK 10?

Take a look at the somewhat complicated relationship between Lombok and JDK 10. While it's boon to reduce boilerplate code, Lombok and Java 10's var can fill a couple of the same gaps.

Mark Heckler user avatar by
Mark Heckler
·
Jun. 08, 18 · Tutorial
Like (4)
Save
Tweet
Share
8.69K Views

Join the DZone community and get the full member experience.

Join For Free

Lombok is one of those libraries that plugs several small gaps in fairly basic Java functionality. It covers so many small gaps, in fact, that it's easy to use it to provide some very useful capabilities for years while overlooking other capabilities it quietly provides just as well.

This happened to me recently, as I mentioned in the tweet below. Caveat: I'm for code clarity first and foremost, so I don't recommend sacrificing readability... but if there is a way to be more concise, i.e. stating/coding something with less text while retaining full context, I'm for it.

Enter JDK 10's var option for declaring local variables, courtesy of Project Amber and JEP 286. If you're not using Kotlin (or Groovy, Scala, Javascript, etc.) in your shop, this small detail may hold some appeal for you. But it's (to date) only available in JDK 10... are you and your team using JDK 10 yet? Chances are, you aren't. Ah, but what about Lombok?

Ignoring all the other things it can do, Lombok also enables you to declare local variables as values (immutable) and variables (mutable). Here's how:

To declare local immutable values, just use the val "keyword" (actually a Lombok type of sorts) where you would normally provide the type declaration. Lombok's var works similarly, except it creates a mutable type.

@Data 
@AllArgsConstructor 
class MyClass { 
    public void logIt() { 
        val logMessage = "This is my sweet log message"; 
        var logStatus = 909; 
        System.out.println("Log status: " + logStatus + ", message: " + logMessage); 
    } 
}


The relevant import statements will likely be suggested by your IDE, but here they are for reference:

import lombok.experimental.var; 
import lombok.val; 


The first time you try to compile your class using var, you'll see this error message:

Error:(37, 13) java: Use of var is disabled by default. Please add 'lombok.var.flagUsage = ALLOW' to 'lombok.config' if you want to enable is. 


This is easily resolved by creating a file called lombok.config in your project's root and adding the single entry lombok.var.flagUsage = ALLOW, as recommended. Subsequent builds should perform as expected.

Click here to follow me on Twitter for more quick tips like this. Happy coding!

Useful Links

  • Lombok features
  • JEP 286
Java (programming language) Java Development Kit Coding (social sciences)

Published at DZone with permission of Mark Heckler, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • An Introduction to Data Mesh
  • The Role of Data Governance in Data Strategy: Part II
  • Distributed SQL: An Alternative to Database Sharding
  • How to Secure Your CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: