Final Keyword in Java – Java Final Variable, Final Method, Final Class

1. Objective

In our last Java tutorial, we studied Static Method in Java in detail. Today, we come with a new topic called Final Keyword in Java Programming. In this Final Keyword in Java Tutorial, we will study Java Final Variable, Java Final Class, and Java Final Method. Moreover, we will study when to use a final variable in Java and reference final variable in Java.

So, let’s start Final Keyword in Java.

Final Keyword in Java - Java Final Variable, Final Method, Final Class

Final Keyword in Java – Java Final Variable, Final Method, Final Class

2. Final Keyword in Java

Final keyword in Java is employed in several contexts. 1st of all, final may be a non-access modifier applicable solely to a variable, a way or a class. Followings are totally different contexts wherever final use.

When a variable is asserted with a Java final keyword, its value can’t change, essentially, a constant. This also means that you simply must initialize a Java final variable. If the final variable reference, this suggests that the variable can’t be re-bound to reference another object. However, an internal state of the thing pointed by that reference variable is changed i.e. you’ll be able to add or remove elements from a final array or final collection. It’s good practice to represent final variables in all uppercase, using an underscore to separate words.

Do you know the Top 4 Java Keywords?

Example-
final int THRESHOLD = 5;
final int THRESHOLD;
static final double PI = 3.141592653589793;
static final double PI;

a. Java Final Variable

We should initialize a final variable in Java, otherwise, a compiler can throw a compile-time error. A Java final variable will only initialize once, either via an initializer or an assignment statement.
There are 3 ways to initialize a Java final variable:

  • You can initialize a final variable when it’s declared. This approach is that the most common. A final variable is named blank final variable if it’s not initialized whereas declaration. Below are the 2 ways that to initialize a blank final variable.
  • A blank final variable is initialized within instance-initializer block or within a constructor. If you have got quite one constructor in your class then it should initialize in all of them, otherwise, a compile-time error is thrown.
  • A blank final static variable is initialized within a static block.

Let’s Learn Java String, Methods, and Constructor – Syntax and Example

Let us see above different ways of initializing a Java final variable through an example.

class Gfg
{
final int THRESHOLD = 5;
final int CAPACITY;
final int MINIMUM;
static final double PI = 3.141592653589793;
static final double EULERCONSTANT;
{
capacity = 25;
}
static
EULERCONSTANT = {2.3;
}
public GFG()
}

i. When to use a Final Variable in Java

The only difference between a normal variable and a Java final variable is that we will re-assign value to a normal variable. However, we tend to cannot change the value of a final variable once assigned. Therefore, a Java final variables should use just for the values that we would like to stay constant throughout the execution of a program.

Final Keyword in Java- Java Final Variable

Final Keyword in Java- Java Final Variable

ii. Reference Final Variable in Java

When a Java final variable may a reference to an object, then this final variable known as reference final variable in Java. For example, a final StringBuffer variable seems like

Read about String vs StringBuffer vs StringBuilder in Java

Final StringBuffer sb;

As you recognize that a final variable can’t re-assign. However, the object of a reference final variable, an internal state of the article point by that reference variable change. Note that this can not re-assigning. This property of final known non-transitivity.

class Gfg
public static void main(String[] args)
{
final StringBuilder sb = new StringBuilder( internal state of object
sb.append("ForFlair");
System.out.println(sb);
}
}

Output-
Flair
ForFlair
Note:- As discussed on top of, a final variable can’t reassign doing it’ll throw a compile-time error.

class Gfg
{
static final int capacity = 4;
Public static void main(String args[])
{
capacity = 5;
}
}

Output-
Compiler Error: Cannot assign a value to a final variable capability.
When a Java final variable, create inside a method/constructor/block, refer as a local final variable, and it should initialize once where create. See below program for a native final variable.

class Gfg
{
public static void main(String args[])
{
final int i;
i = 20;
System.out.println(i);
}
}

Output-
20
Note the distinction between C++ const variables and Java final variables. Const variables in C++ should assign a value once declared. For final variables in Java, it’s not necessary as we tend to see in on top of examples. A final variable assign price later, however, one time.

Read about Loops in Java – Types and Examples of Looping in Java

Final with a foreach loop: final with for-each statement may a legal statement.

class Gfg
;
for (final int i: arr)
System.out.print(i + " ");
}
}

Output-
1 2 3

b. Java Final Class

When a class asserts with a final keyword, known as Java final class. A final class cannot extend (inherited). There are 2 uses of a final class in Java:
Java Final Class cannot extend so, one is surely preventing inheritance. For example, All wrapper class in java like, float, integer etc. are Java final Class and we cannot extend them.

final class A
}
class B extends A
cannot subclass A
}

The other use of final with classes is to make an immutable class like the predefined String class. You cannot create a class immutable while not creating it finally.

Have a Look at Java Character Class Methods with Syntax and Examples

c. Java Final Method

When a method is declared with a final keyword, it’s called a Java final method. A final method can not overridden. The object class will this—a number of its methods are final. We should declare strategies with a final keyword in Java, that we tend to needed to follow constant implementation throughout all the derived classes. The subsequent fragment illustrates final keyword with a method:

class A
{
final void m1()
{
System.out.println("This may be a final method.");
}
}
class B extends A
void m1()
{
System.out.println("Illegal!");
}

So, this was all about Final Keyword in Java. Hope you like our explanation.

3. Conclusion

Hence, in this Java Final Keyword Tutorial, we studied what is a final keyword in Java: Java Final Variable, Java Final Methods, Java Final Class. Furthermore, if you have any query regarding Final Keyword in Java, feel free to ask in the comment box.

See Also- Java Garbage Collection Algorithm 

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *