Classes and enums with private members should have a constructor


I’m seeing the following during SonarLint: Classes and enums with private members should have a constructor.

This may be sibling thread titled The reason behind squid:S1258, but this one is more geared toward java:S1258.

As a resolution, it suggests the following:

private members should explicitly initialize those members, either in a constructor or with a default value.

For SpringBoot application, @Value annotation takes care of default value. Even with this annotation, can we consider java:S1258 as valid?

Hello Justin and welcome to the community!

As you might have probably noticed this rule is not activated by default, which means that you in your team/organization can decide whether it’s relevant or not.

As for @Value annotation from Spring, you can either use it on a constructor argument, method, or property itself. If you do so you indeed don’t need to provide a default value to the property.