Class members modifier: final

The keyword final is a modifier that can prefix both class fields and methods. If a variable is prefixed with the modifier final, then it is in reality a constant that can't be modified once initialized with a value. This applies to instance, static and local variables, including parameters that are declared final.

  • A final variable of a primitive data type can't change its value once it has been initialized.
  • A final variable of a reference type can't change its reference value once it has been initialized, but the state of the object it denotes can still be changed.


Note: final variable need not be initialized at its declaration, but it must be initialized once before being used.

A final method in a class is complete (i.e., has an implementation) and can't be overridden in any subclass. That is, a subclass can't change its behavior.

So final variables ensure that values can't be changed and final methods ensure that behavior can't be changed.

Note: Variables declared in an interface are implicitly final.


Class members modifier: static

Members (fields and methods) of a class may be prefixed by the keyword static to distinguish them from instance (i.e.,non-static) members.
Static members belong to the class they are declared in but make no part of any instance of that class.
When the class is loaded, static variables (also called class variables) are initialized either explicitly by an initialization expression or given default values, these values are not part of the state of any object. In other words, static variables are not instantiated if an instance of the class is created.

There is no notion of object associated with static methods (also known as class methods), therefore a static method in a class can't access instance (i.e.,non-static) members. A static method can access directly other static members in the class. However static methods can always use a reference of the class' type to access its static and instance members.

Clients may access static members through the object reference of the class or simply by using the class name. Anyway, the class need not be instantiated to access its static members.