Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/mbizsoftware.com/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math-pro domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/mbizsoftware.com/wp-includes/functions.php on line 6114
What Is The 'final' Keyword In Java & How Can You Use It? - Innovative Software Solution Company | MBiz Software

What Is The ‘final’ Keyword In Java & How Can You Use It?

There are more than fifty reserved words such as keywords and literals that cannot be used as identifiers in java. The ‘final’ keyword in java is a non-access modifier, which is used to control three very specific aspects of classes, methods, and variables. 

The ‘final’ Classes

When a final keyword is added before a class, it makes it a final class. A final class cannot be extended. And also, a final class cannot be abstract due to obvious reasons. In a case where the programmer tries to extend a final class, it will give a compile-time error.  

final class MyFinalClass {  
     //code inside class  
} 
public class MyClass extends MyFinalClass {  
    void test() {  
        System.out.println("My Method");  
    }  
     public static void main(String[] args {  
         A obj = new A();  
        obj.test();  
     }  
}

Using the final keyword, programmers can avoid unauthorized access, and in the meantime protect important information. As an example, there are some logics which are vital to be temper protected as possible. In this case, making the class final prevents the inheritances and so the possibility of tampering also. 

The ‘final’ Methods 

Final methods prevent overriding the specific method in subclasses. Doing so will result in a compile-time error. 

public class MySuperClass {  
  // declaring method as final  
  public final void display() {  
    System.out.println("Hellow World!");  
  }  
}  
public class MySubClass extends MySuperClass {  
  // try to override the final method  
  public void display() {  
    System.out.println("Welcome!");  
  }  
}  

With this, the programmer can minimize unwanted behaviors of child classes depicted from modifications of the methods that tamper with logic. In a real-world scenario, programmers can extend the thread class and create new ones but cannot override the isAlive() method which checks whether a thread is alive. 

The ‘final’ variables 

Final variables cannot be reassigned after the first initialization.  

final String str = "Hello";
str = "world"; // throw compilation error!

final int value = 10;
value++; // throw compilation error!

Blank final variables. 

Final variables can be declared without initialization. 

final String str;
str = "Hello World";

That kind of final variable is identified as a blank final variable. Unlike instance variables these blank final variables don’t have default values, therefore, initialization is mandatory for final variables. Otherwise. Once the final variable is initialized, it cannot be changed. So, the final variable acts as a constant.

In JVM compiled codes can improve the performance at runtime, Final variables improve performance during runtime. For the JVM final variables are compiled code then which will help to improve the performance. And the final variables are useful to keep value without changing in subclasses. But final variables cannot be trusted with non-primitive variables because the state or value can be modified. 

Bonus

As a bonus fact, final global variables are heavily used in dependency injection where initialization happens through a contractor.  

public class MyService { 
    private final MyRepository myRepository; 
    public MyService(MyRepository myRepository) { 
        this.myRepository = myRepository; 
    } 
    //Do Something with my repository 
} 

The ‘final’ parameters 

Parameters are the values that programmers pass to a method from outside to execute a particular task inside the method. Just like the final variables, final parameters also cannot be reassigned in the method.  

public void sampleA(final int value) { 
    // try to modify the value of final parameter variable 
    value += 1; 
    System.out.println("Value is " + value); 
  }

Conclusion 

As discussed above, final classes avoid extension while final methods avoid unwanted behaviors of subclasses and final variables are read-only by design. The final keyword helps keep up the program’s readability while improving its understandability. And also, some performance gains are using the final keyword.  

Share the Post: