rspec/rules/S3066/java/rule.adoc

44 lines
912 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
``++enum++``s are generally thought of as constant, but an ``++enum++`` with a ``++public++`` field or ``++public++`` setter is non-constant. Ideally fields in an ``++enum++`` are ``++private++`` and set in the constructor, but if that's not possible, their visibility should be reduced as much as possible.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
public enum Continent {
NORTH_AMERICA (23, 24709000),
// ...
EUROPE (50, 39310000);
public int countryCount; // Noncompliant
private int landMass;
Continent(int countryCount, int landMass) {
// ...
}
public void setLandMass(int landMass) { // Noncompliant
this.landMass = landMass;
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
public enum Continent {
NORTH_AMERICA (23, 24709000),
// ...
EUROPE (50, 39310000);
private int countryCount;
private int landMass;
Continent(int countryCount, int landMass) {
// ...
}
----