2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-06-08 14:23:48 +02:00
It is both suspicious and unusual to make direct assignments to super-class fields from a class constructor. Choosing to make such direct assignments, rather than invoking the ``++super++`` constructor that sets them, potentially bypasses important initialization.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
public class Parent {
protected int start;
public Parent() {
//...
}
public Parent(int start) {
this.start = start;
}
}
public class Child extends Parent {
public Child(int start) {
this.start = start; // Noncompliant
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2021-06-08 14:23:48 +02:00
----
public class Parent {
protected int start;
public Parent() {
//...
}
public Parent(int start) {
this.start = start;
}
}
public class Child extends Parent {
public Child(int start) {
super(start);
}
}
----