Language syntax

Java Final class declaration

A final class cannot be extended. This alone does not make its fields immutable.

Formal Syntax & Structure

java syntaxjava
final class GridCell { /* members */ }

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());    }}
Java Reference