final Classes, Methods and
VariablesPurpose:
- 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
finalmodifier can be used with variables to create constants. A value must be assigned to the variable at declaration, and then cannot be changed. - A
finalmethod cannot be redefined in a subclass. - A
finalclass cannot be subclassed.
email suggestions to: cs015tas@cs.brown.edu
