[Java] How to omit the private constructor in Lombok

Make a note of the technique used by seniors at work

1. What is Lombok?

This is a convenient library that automatically generates boilerplate code (a standard code that cannot be omitted due to language specifications) at compile time. For example, JavaBean getters / setters can be annotated.

2. What you want to do

I want to prevent instantiation with a Utility-like class that has only static methods!

3. Create a private constructor

3-1. General private constructor

Effective Java also has a memory that was described like this.


public class HogeUtils {

  private HogeUtils() {
    // some exception

  // some static method


3-2. Private constructor omitted in Lombok

Just annotate @NoArgsConstructor to the class: angel:


@NoArgsConstructor(access=AccessLevel.PRIVATE) // <-here
public class HogeUtils {

  // some static method


4. What do you mean?

@NoArgsConstructor Annotation to generate the default constructor. [TERASOLUNA Server Framework for Java (5.x) Development Guideline | 11.2. Elimination of Boilerplate Code (Lombok)](http://terasolunaorg.github.io/guideline/5.3.1.RELEASE/en/Appendix/Lombok .html)

As the name suggests, the option ʻaccess = AccessLecel.PRIVATE` sets the access modifier privately. With this annotation, you'll be generating the same code you would normally do.

Lombok Wow, that's it.

Recommended Posts

[Java] How to omit the private constructor in Lombok
[Java] How to omit spring constructor injection with Lombok
Studying how to use the constructor (java)
How to get the date in java
How to use Lombok in Spring
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
How to concatenate strings in java
How to get the class name / method name running in Java
How to derive the last day of the month in Java
How to switch Java in the OpenJDK era on Mac
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
Java reference to understand in the figure
[Java] How to use the hasNext function
[Java] How to use the HashMap class
How to do base conversion in Java
[Java] How to use the toString () method
[Processing × Java] How to use the loop
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to implement coding conventions in Java
How to embed Janus Graph in Java
[Processing × Java] How to use the class
How to install the legacy version [Java]
[Processing × Java] How to use the function
How to solve the unknown error when using slf4j in Java
[Java] How to use the Calendar class
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to test a private method in Java and partially mock that method
How to find the total number of pages when paging in Java
How to get the value after "_" in Windows batch like Java -version
How to get the absolute path of a directory running in Java
How to create your own annotation in Java and get the value
How to check the logs in the Docker container
[Java] How to use Thread.sleep to pause the program
How to display a web page in Java
How to add sound in the app (swift)
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] (for MacOS) How to set the classpath
How to use the replace () method (Java Silver)
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
[Java] How to get the redirected final URL
How to build the simplest blockchain in Ruby
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
How to write Java String # getBytes in Kotlin?
How to check Rails commands in the terminal
How to access Java Private methods and fields
How to automatically generate a constructor in Eclipse
How to set when "The constructor Empty () is not visible" occurs in junit
[Java] How to get the URL of the transition source
How to call functions in bulk with Java reflection
How to set the display time to Japan time in Rails