100

Is there any annotation available for Lombok private NoArgConstructor? Lombok @NoArgConstructor creates a public Constructor with no parameter. But I want private constructor annotation.

4
  • What about if I want to create a singleton class? Commented May 23, 2020 at 23:49
  • 1
    @emon The usual current practice is to use an enum. Commented May 24, 2020 at 0:14
  • 1
    @chrylis-cautiouslyoptimistic- thanks. Commented May 24, 2020 at 0:52
  • 3
    In the case when you have only static methods you can use the experimental @UtilityClass. "By marking your class with @UtilityClass, lombok will automatically generate a private constructor...". Commented Mar 12, 2023 at 19:26

1 Answer 1

190

Use the access level element in the annotation. @NoArgsConstructor(access = AccessLevel.PRIVATE)

Sign up to request clarification or add additional context in comments.

3 Comments

Funny because that has more characters than actually typing the private constructor
@GabrielBB Depends on the class name right? And also it takes more lines of code if you have a style checker that enforces that braces are in separate lines.
I agree, but typing all the getters, setters and constructors is way longer than the annotation. But if you use lombok in one place (where it shortens the syntax), typing the private no args constructor makes your code non-consistent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.