Is there any annotation available for Lombok private NoArgConstructor? Lombok @NoArgConstructor creates a public Constructor with no parameter. But I want private constructor annotation.
-
What about if I want to create a singleton class?emon– emon2020-05-23 23:49:34 +00:00Commented May 23, 2020 at 23:49
-
1@emon The usual current practice is to use an enum.chrylis -cautiouslyoptimistic-– chrylis -cautiouslyoptimistic-2020-05-24 00:14:55 +00:00Commented May 24, 2020 at 0:14
-
1@chrylis-cautiouslyoptimistic- thanks.emon– emon2020-05-24 00:52:23 +00:00Commented May 24, 2020 at 0:52
-
3In 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...".Trayan Momkov– Trayan Momkov2023-03-12 19:26:54 +00:00Commented Mar 12, 2023 at 19:26
Add a comment
|
1 Answer
Use the access level element in the annotation. @NoArgsConstructor(access = AccessLevel.PRIVATE)
3 Comments
GabrielBB
Funny because that has more characters than actually typing the private constructor
Guillermo
@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.
Milan Panic
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.