final Classes, Methods and Variables

Purpose:

To declare a method or variable that cannot be over-written, or to create a class that cannot be extended.

Mechanics:

final <other modifiers> <data type> <constant name> = <constant value>;

final <other modifiers> <return type> <method name>(<parameter list>) {
/* Java statements */
}

final <other modifiers> class <class name> {
/* Instance variables declared and methods defined */
}

Example:

final int constantNum = 1;

final void myMethod() {
/* Java statements */
}

final class myClass {
/* Instance variables declared and methods defined here */
}

Usage:

  • The final modifier can be used with variables to create constants. A value must be assigned to the variable at declaration, and then cannot be changed.
  • A final method cannot be redefined in a subclass.
  • A final class cannot be subclassed.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu