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

46 lines
1.3 KiB
HTML

<html>
<body>
Reports fields that can be safely made <code>final</code>.
All <code>final</code> fields have a value and this value does not change, which can make the code easier to reason about.
<p>To avoid too expensive analysis, this inspection only reports if the field has a <code>private</code> modifier
or it is defined in a local or anonymous class.
A field can be <code>final</code> if:
<ul>
<li>It is <code>static</code> and initialized once in its declaration or in one <code>static</code> initializer.</li>
<li>It is non-<code>static</code> and initialized once in its declaration, in one instance initializer or in every constructor</li>
</ul>
And it is not modified anywhere else.
<p><b>Example:</b></p>
<pre><code>
public class Person {
private String name; // can be final
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
public class Person {
private final String name;
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
</code></pre>
<!-- tooltip end -->
<p>
Use the "Annotations" button to modify the list of annotations that assume implicit field write.
</p>
</body>
</html>