8.13. final Instance Variables

The principle of least privilege is fundamental to good software engineering. In the context of an application, the principle states that code should be granted only the amount of privilege and access that it needs to accomplish its designated task, but no more. Let us see how this principle applies to instance variables.

Some instance variables need to be modifiable and some do not. You can use the key-word final to specify that a variable is not modifiable (i.e., it is a constant) and that any attempt to modify it is an error. For example,

private final int INCREMENT;

declares a final (constant) instance variable INCREMENT of type int. Although constants can be initialized when they are declared, this is not ...

Get Java™ How to Program, Seventh 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.