Final (Java) - Final Methods

Final Methods

A final method can't be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.

Example:

public class MyClass { public void myMethod {...} public final void myFinalMethod {...} } public class AnotherClass extends MyClass { public void myMethod {...} // Ok public final void myFinalMethod {...} // forbidden }

A common misconception is that declaring a class or method final improves efficiency by allowing the compiler to directly insert the method inline wherever it is called. In fact the compiler is unable to do this because the method is loaded at runtime and might not be the same version as the one that was just compiled. Only the runtime environment and JIT compiler have the information about exactly which classes have been loaded, and are able to make better decisions about when to inline, whether or not the method is final.

Read more about this topic:  Final (Java)

Famous quotes containing the words final and/or methods:

    Life is a series of diminishments. Each cessation of an activity either from choice or some other variety of infirmity is a death, a putting to final rest. Each loss, of friend or precious enemy, can be equated with the closing off of a room containing blocks of nerves ... and soon after the closing off the nerves atrophy and that part of oneself, in essence, drops away. The self is lightened, is held on earth by a gram less of mass and will.
    Coleman Dowell (1925–1985)

    In inner-party politics, these methods lead, as we shall yet see, to this: the party organization substitutes itself for the party, the central committee substitutes itself for the organization, and, finally, a “dictator” substitutes himself for the central committee.
    Leon Trotsky (1879–1940)