The final keyword in Java is deeply understood

  • 2020-06-12 08:55:55
  • OfStack

java final

Preface:

The final keyword in Java is important and can be applied to classes, methods, and variables. In this article I will show you what the final keyword is. What does it mean to declare variables, methods and classes as final? What are the benefits of using final? Finally, there are some examples of using the final keyword. final is often used with static1 to declare constants, and you will see how final improves application performance.

final keyword meaning ?

final is a reserved keyword in Java for declaring member variables, methods, classes, and local variables. Once you declare the reference as final, you will not be able to change the reference, the compiler will check the code, and if you try to initialize the variable again, the compiler will report a compilation error.

What is the final variable?

Anything that is declared as final to a member variable or a local variable (called a local variable in a method or block of code) is called final. The final variable is often used as a constant with the static keyword 1. Here is an example of the final variable:


public static final String LOAN = "loan";
LOAN = new String("loan") //invalid compilation error

The final variable is read-only.

What is the final method ?

final can also declare methods. The final keyword precedes a method to indicate that the method cannot be overridden by a subclass method. You can declare the method final if you think the functionality of the method is complete enough that the subclass does not need to be changed. final methods are faster than non-ES47en methods because they are already statically bound at compile time and do not need to be dynamically bound at run time. Here is an example of the final method:


class PersonalLoan{
  public final String getName(){
    return "personal loan";
  }
}
 
class CheapPersonalLoan extends PersonalLoan{
  @Override
  public final String getName(){
    return "cheap personal loan"; //compilation error: overridden method is final
  }
}

What is the final class?

The class decorated with final is called the final class. final classes are generally functional and cannot be inherited. There are many classes in Java that are final, such as String, Interger, and other packaging classes. Here is an example of the final class:


 final class PersonalLoan{
 
  }
 
  class CheapPersonalLoan extends PersonalLoan{ //compilation error: cannot inherit from final class
 
}

Benefits of the final keyword

The benefits of using the final keyword are summarized below

The final keyword improves performance. Both JVM and Java applications cache the final variable. The final variable can be safely Shared in a multithreaded environment without additional synchronization overhead. Using the final keyword, JVM optimizes methods, variables, and classes.

Immutable classes

The final keyword is used to create immutable classes. Immutable classes are objects that cannot be changed once they are created. String is representative of immutable classes. Immutable classes have many benefits, such as that their objects are read-only, can be safely Shared in a multithreaded environment, without additional synchronization overhead, and so on.

Important points about final

The final keyword can be used for member variables, local variables, methods, and classes. The final member variable must be initialized at declaration time or in the constructor, otherwise a compilation error will be reported. You cannot assign another value to the final variable. Local variables must be assigned at declaration time. All variables in the anonymous class must be final variables. The final method cannot be overridden. The final class cannot be inherited. The final keyword differs from the finally keyword, which is used for exception handling. The final keyword is easily confused with the finalize() method, which is defined in the Object class and is called by JVM before garbage collection. All variables declared in the interface are themselves final. The final and abstract keywords are inversely related, so the final class cannot be abstract. The final method is bound at compile time and is called static binding (static binding). Those that do not initialize the final variable at declaration time are called blank final variables (blank final variable) and must be initialized in the constructor or call this() initialization. If you don't, the compiler says "final variable needs to be initialized." Declaring classes, methods, variables as final improves performance so that JVM has a chance to estimate and then optimize. According to the Java code convention, the final variable is a constant, and usually the constant name is capitalized:

private final int COUNT = 10;

Declared final for collection objects means that references cannot be changed, but you can add, delete, or change content to them. Such as:


private final List Loans = new ArrayList();
list.add( " home loan " ); //valid
list.add("personal loan"); //valid
loans = new Vector(); //not valid

We already know what the final variable, the final method, and the final class are. Use final when necessary to write faster and better code.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: