How to work with the final keyword

The final key word can be used on the following:

  1. classes
  2. methods
  3. parameters

You can use the keyword whenever you want to make sure that no one will override or change your classes, methods, or parameters.

Example final class – the class can not be inherited.

public final classs book extends Product
{
// all methods in the class are automatically final
}

Example final method – the method can not be overriden

public final String getVersion()
{
return version;
}

Example final parameter – prevent a method from assigning a new value to a parameter.

public void setVersion(final String version)
{
// version = "new value"; // not allowed
this.version = version;
}