Files
openide/java/java-impl/resources/inspectionDescriptions/CompareToUsesNonFinalVariable.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

42 lines
1.1 KiB
HTML

<html>
<body>
Reports access to a non-<code>final</code> field inside a <code>compareTo()</code> implementation.
<p>
Such access may result in <code>compareTo()</code>
returning different results at different points in the object's lifecycle, which may in turn cause problems when
using the standard collections classes, for example <code>java.util.TreeSet</code>.
</p>
<p>
A quick-fix to make the field <code>final</code> is available
only when there is no write access to the field, otherwise no fixes are suggested.
</p>
<p><b>Example:</b></p>
<pre><code>
class Foo implements Comparable&lt;Foo&gt;{
private int index;
Foo(int idx) {
index = idx;
}
@Override
public int compareTo(Foo foo) {
return Integer.compare(this.index, foo.index);
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
class Foo implements Comparable&lt;Foo&gt;{
private final int index;
Foo(int idx) {
index = idx;
}
@Override
public int compareTo(Foo foo) {
return Integer.compare(this.index, foo.index);
}
}
</code></pre>
<!-- tooltip end -->
</body>
</html>