Home » Java Tutorial » Final Keyword in Java

Final Keyword in Java

One question can arise that exactly “what is a final keyword in java?” The answer to this related question is “The final keyword in java is a non-access modifier which provides its users with little conditions or limits. In several other sentences, we do not reallocate or reinitialize them when we can use the main-final keyword. This implies that we can’t modify beliefs.” 

In various ways, this keyword is being used. It is utilized mainly for the execution of its respective class, methodologies, variables, etc. Let’s discuss each of them in detail.

Final variables utilized in java

The thing is possible to directly assign a final variable just once. It is never possible to reallocate a reference variable proclaimed final to refer to another entity.

The information inside the entity may, though, be modified. So, it’s possible to alter the state of an object, but maybe not the relation.

For variables, to enable the constant a class variable, the modifier final in java is always utilized with the keyword static.

It is considered professional when we utilize the final keyword in java in uppercase letters and utilizing underscore in case we need to divide.

Let’s have a grasp on the illustration that makes usage of a final variable in java:

public class demo_fj1 {
  public static void main(String args[]) {
    // Let’s construct a java final value or variable
    final int price_fj = 89;
    // Let’s try to adjust the variable that we declared final 
    price_fj = 18;
    System.out.println("The price_fj is”: " +price_fj);


// demo_fj1.java:6-- error: cannot allow a value to declared final variable
// price_fj
//    price_fj = 18;
//1 error

How to initialize the java final variable?

A final variable that is formed by the help of the java final keyword must’ve been initialized, or the compiler can trigger a compile-time failure. We could only assign a final variable once, either through an initializer or an assignment expression. There have been 3 methods a final variable can be initialized: 

A final variable may be configured when that is specified. That’s the most common technique. A final variable, if not configured during the statement, is considered a blank final variable. The two different ways to configure a blank final variable are below:

  1. Inside the illustration block or the constructor, an empty final variable can be configured. If you already have more than 1 java constructor in your category, it should be loaded in all of them, otherwise, there would be a compile-time failure.
  2. And inside the static block, an empty final static variable can also be configured.

In this way, we can make use of the final keyword in java in various ways. The other ways that are the methodology and class are further discussed below.

Final methodology in java

When we make utilization of the final keyword in java, before some procedure, then it is termed as java final methodology or procedure. It means or states that after declaring that procedure as final, we can’t override that final procedure. We should have some information regarding polymorphism and inheritance for realizing this process. When there are identical methodologies owned by base or parent and child or subclass, then we term that concept as “Methodology overriding“.Let’s have a grasp on the final keyword in java with an example utilizing the java final methodology.

class fj_method1 {
    // Let’s construct some final methodology
    public final void fj_disp() {
      System.out.println("This methodology is final methodology of java");
class fj_method2 extends fj_method1 {
  // Let’s make a try to override the fj_method1 that is declared final
  public final void fj_disp() {
    System.out.println("Oops!! The above declared final methodology is being overridden!");
  public static void main(String args[]) {
    fj_method2 fj = new fj_method2();


// fj_disp() in fj_method2 cannot override fj_disp() in fj_method1
//   public final void fj_disp() {
//                    ^
//   overridden methodology is final

The final class in java

When some class or classes are configured with the final keyword then that class or classes is referred to as the final class in java. The final class in java can’t be extended.

Let’s have a quick eye on the illustration below that represents the utilization of the final class in java.

// Let’s construct some final class in java
final class fc_demo1 {
  public void fc_disp() {
    System.out.println("Hello! This methodology is termed final.");
// Let’s make a try to extend above declared final java class
class fc_demo2 extends fc_demo1 {
  public  void fc_disp() {
    System.out.println("Oops! The final methodology is being overridden!");
  public static void main(String args[]) {
    fc_demo2 fc = new fc_demo2();


// cannot inherit from final fc_demo1
// class fc_demo2 extends fc_demo1 {
//                   ^
Do you know?
1. Substring Java
2. Java Switch Statement with Examples
3. Java Queue
4. String to Int Java
5. Super Keyword in Java
6. Fibonacci series in java
7. HashSet in Java with Examples
8. Math.random Java
9. Static Keyword in Java
10. Final Keyword in Java

Pin It on Pinterest