Final Classes, Methods, and Variables

The final modifier is used with classes, methods, and variables to indicate that they will not be changed. It has different meanings for each thing that can be made final, as follows:

  • A final class cannot be subclassed.

  • A final method cannot be overridden by any subclasses.

  • A final variable cannot change in value.

Variables

You got a chance to work with final variables during Day 6. They are often called constant variables (or just constants) because they do not change in value at any time.

With variables, the final modifier often is used with static to make the constant a class variable. If the value never changes, you don't have much reason to give each object in the same class its own copy of the value. They ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.