Language syntax
Java Final instance field
An instance field marked final can be assigned only as allowed by definite assignment rules; once initialized it cannot be reassigned. A reference stored there may still designate a mutable object.
Formal Syntax & Structure
java syntaxjava
private final int row;Working Example
Example Snippetjava
final class GridCell { private final int row; private final int column; GridCell(int row, int column) { if (row < 0 || row > 999 || column < 0 || column > 999) throw new IllegalArgumentException("cell outside grid"); this.row = row; this.column = column; } public int row() { return row; } public int column() { return column; } public GridCell nextColumn() { return new GridCell(row, column + 1); } @Override public boolean equals(Object other) { if (!(other instanceof GridCell cell)) return false; return row == cell.row && column == cell.column; } @Override public int hashCode() { return 1000 * row + column; }}public class Main { public static void main(String[] args) { GridCell first = new GridCell(2, 4); GridCell next = first.nextColumn(); System.out.println(first.column()); System.out.println(next.column()); }}