mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
36 lines
860 B
HTML
36 lines
860 B
HTML
<html>
|
|
<body>
|
|
Reports assignment to, or modification of fields that are declared in a superclass from within a subclass constructor.
|
|
<p>It is considered preferable to initialize the fields of a superclass in its own constructor and
|
|
delegate to that constructor in a subclass. This will also allow declaring a field <code>final</code>
|
|
if it isn't changed after the construction.</p>
|
|
<p><b>Example:</b></p>
|
|
<pre><code>
|
|
class Super {
|
|
int x;
|
|
}
|
|
class Sub extends Super {
|
|
Sub(int _x) {
|
|
// Warning: x is declared in a superclass
|
|
x = _x;
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>To avoid the problem, declare a superclass constructor:</p>
|
|
<pre><code>
|
|
class Super {
|
|
final int x;
|
|
|
|
Super(int _x) {
|
|
x = _x;
|
|
}
|
|
}
|
|
class Sub extends Super {
|
|
Sub(int _x) {
|
|
super(_x);
|
|
}
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html> |