@@ -5,8 +5,8 @@ <h2>Why is this an issue?</h2>
55< p > Traditionally, the explicit constructor invocation (< code > super(…)</ code > or < code > this(…)</ code > ) had to be the first statement in a
66constructor, forcing subclass field initialization to happen after the superclass was already constructed. If the superclass constructor calls an
77overridable method, the subclass implementation will see default values (such as < code > null</ code > , < code > 0</ code > , or < code > false</ code > ) for its
8- fields instead of the values intended by the caller. This leads to subtle bugs, ` NullPointerException` s, or inconsistent object states that are
9- difficult to debug.</ p >
8+ fields instead of the values intended by the caller. This leads to subtle bugs, < code > NullPointerException</ code > s, or inconsistent object states that
9+ are difficult to debug.</ p >
1010< h2 > How to fix it</ h2 >
1111< p > Move the initialization of subclass fields before the < code > super()</ code > call. This takes advantage of flexible constructor bodies to ensure that
1212the subclass state is established before the superclass constructor begins its execution. Alternatively, if the method in the superclass does not need
@@ -16,10 +16,10 @@ <h4>Noncompliant code example</h4>
1616< pre data-diff-id ="1 " data-diff-type ="noncompliant ">
1717class Super {
1818 Super() {
19- overriddenMethod ();
19+ foo ();
2020 }
2121
22- void overriddenMethod () {
22+ void foo () {
2323 System.out.println("Base logic");
2424 }
2525}
@@ -29,11 +29,11 @@ <h4>Noncompliant code example</h4>
2929
3030 Sub(int x) {
3131 super();
32- this.x = x; // Noncompliant: x is uninitialized when overriddenMethod is called by Super()
32+ this.x = x; // Noncompliant: x is uninitialized when foo is called by Super()
3333 }
3434
3535 @Override
36- void overriddenMethod () {
36+ void foo () {
3737 System.out.println(x); // Prints 0 instead of the value of x
3838 }
3939}
@@ -42,10 +42,10 @@ <h4>Compliant solution</h4>
4242< pre data-diff-id ="1 " data-diff-type ="compliant ">
4343class Super {
4444 Super() {
45- overriddenMethod ();
45+ foo ();
4646 }
4747
48- void overriddenMethod () {
48+ void foo () {
4949 System.out.println("Base logic");
5050 }
5151}
@@ -59,21 +59,21 @@ <h4>Compliant solution</h4>
5959 }
6060
6161 @Override
62- void overriddenMethod () {
62+ void foo () {
6363 System.out.println(x); // Prints the expected value
6464 }
6565}
6666</ pre >
6767< p > Alternatively, if the method in the superclass does not need to be overridden, it can be marked as < code > final</ code > or < code > private</ code > to
6868prevent the issue entirely.</ p >
6969< h4 > Noncompliant code example</ h4 >
70- < pre data-diff-id ="1 " data-diff-type ="noncompliant ">
70+ < pre data-diff-id ="2 " data-diff-type ="noncompliant ">
7171class Super {
7272 Super() {
73- overriddenMethod ();
73+ foo ();
7474 }
7575
76- void overriddenMethod () {
76+ void foo () {
7777 System.out.println("Base logic");
7878 }
7979}
@@ -83,23 +83,23 @@ <h4>Noncompliant code example</h4>
8383
8484 Sub(int x) {
8585 super();
86- this.x = x; // Noncompliant: x is uninitialized when overriddenMethod is called by Super()
86+ this.x = x; // Noncompliant: x is uninitialized when foo is called by Super()
8787 }
8888
8989 @Override
90- void overriddenMethod () {
90+ void foo () {
9191 System.out.println(x); // Prints 0 instead of the value of x
9292 }
9393}
9494</ pre >
9595< h4 > Compliant solution</ h4 >
96- < pre data-diff-id ="1 " data-diff-type ="compliant ">
96+ < pre data-diff-id ="2 " data-diff-type ="compliant ">
9797class Super {
9898 Super() {
99- overriddenMethod ();
99+ foo ();
100100 }
101101
102- final void finalMethod () {
102+ final void foo () {
103103 System.out.println("Base logic");
104104 }
105105}
@@ -109,13 +109,13 @@ <h4>Compliant solution</h4>
109109
110110 Sub(int x) {
111111 super();
112- this.x = x; // Compliant: finalMethod is final, so it cannot be overridden and will not access uninitialized fields
112+ this.x = x; // Compliant: foo is final, so it cannot be overridden and will not access uninitialized fields
113113 }
114114}
115115</ pre >
116116< h2 > Resources</ h2 >
117117< h3 > Documentation</ h3 >
118118< ul >
119- < li > < a href ="https://openjdk.org/jeps/513 "> JEP 513: Flexible Constructor Bodies</ a > </ li >
119+ < li > < a href ="https://openjdk.org/jeps/513 "> JEP 513: Flexible Constructor Bodies</ a > </ li >
120120</ ul >
121121
0 commit comments