Java final keyword

Facebooktwitterredditpinterestlinkedinmailby feather

Java final keyword

The ‘final’ keyword is a non-access modifier. It is used to restrict the user to manipulate the data members (variables, methods, and classes).

It is used in different context like

  • Variables
  • Methods
  • Classes

Final variables

A variable which is declared with the ‘final’ keyword is called a final variable. If you want to declare a variable as final then prefix a keyword ‘final’ with it. Like final int a, final string s, final char c, etc.

We can’t modify the value of a final variable although we can re-assign value to a normal variable. So we use the final variable for the values which we want to remain constant throughout the program. A final variable should be initialized.

Let’s take a tour with the following examples

Example 1

Output

In the given example, we made the value of pi as final, so it can’t be changed or modified in this program. If we do so, then it will produce a compilation error.

Let’s have a look at another example.

Example 2

Output

Blank final variable

A variable which is not initialized at the declaration time is known as the blank final variable. We must initialize a variable either at the time of declaration or in the constructor of the class; otherwise, it will throw a compilation error. Like var_name might not have been initialized.

Example

Output

Use of blank final variable

Let’s assume we have a college data which is containing a field called Roll_no. Since Roll_no is a fixed entity, so it should not be changed once the student is registered. We can declare Roll_no as a final variable in a class, but we can’t initialize it in advance for all students, because all students have a unique value. In such a case, we can declare roll_no as a blank final variable, and we will initialize this value during object creation.

Example

Output

Final methods

When a method declared with a ‘final’ keyword it is called as final method. If a method declared as final then we can’t override it.

We must declare methods as final when we require the same implementation throughout the program.

Let’s have a look at the below example to understand the final keyword with methods.

Example 1

We can use the parent class final method in child class without any issue

Output

Example 2

Output

The given example will throw the compile-time error because we are trying to override a final method. However, we can use the parent class final method in child class without any issue (as in example 1).

Final classes

A class declared with final keyword is known as final class. A final class can’t be inherited. The final keyword with class prevents it to be inherited.

We create a class final to prevent it from being inherited and make it immutable.

Uses of the final class

There are mostly two uses of a final class.

  • One is to restrict a class to be inherited.
  • And the other use is to make a class as immutable like predefined String class.

Example 1

Output

Example 2

Output

Facebooktwitterredditpinterestlinkedinmailby feather