English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Deep Understanding of the final keyword in Java

 java final keyword in detail:

Preface:

The final keyword in Java is very important and can be applied to classes, methods, and variables. In this article, I will take you through what the final keyword is, what it means to declare variables, methods, and classes as final, what are the benefits of using final, and finally, some examples of using the final keyword. Final is often used with static to declare constants, and you will also see how final improves application performance.

The meaning of the final keyword?

The final keyword in Java is a reserved keyword that can be used to declare member variables, methods, classes, and local variables. Once you declare a reference as final, you cannot change this reference anymore; the compiler will check the code, and if you try to reinitialize the variable, the compiler will report a compilation error.

What is a final variable?

All member variables or local variables (variables within methods or code blocks are called local variables) declared as final are called final variables. Final variables are often used with the static keyword as constants. Here is an example of a final variable:

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

Final variables are read-only.

What is a final method?63;

Final can also be used to declare methods. Adding the final keyword to a method indicates that this method cannot be overridden by a subclass method. If you think a method's functionality is complete enough and does not need to be changed in the subclass, you can declare this method as final. Final methods are faster than non-final methods because they are statically bound at compile time and do not need to be dynamically bound at runtime. Here is an example of a 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 a final class?

A class that is decorated with final is called a final class. Final classes are usually complete in functionality and cannot be inherited. Java has many final classes, such as String, Interger, and other wrapper classes. Here is an example of a final class:

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

  Benefits of the final keyword

Below are some of the benefits of using the final keyword

  1. The final keyword improves performance. Both the JVM and Java applications will cache final variables.
  2. Final variables can be safely shared in a multithreaded environment without additional synchronization overhead.
  3. Using the final keyword, the JVM will optimize methods, variables, and classes.

Immutable class

To create an immutable class, you must use the final keyword. An immutable class refers to an object that, once created, cannot be changed. String is an example of an immutable class. Immutable classes have many benefits, such as their objects being read-only, safe to share in a multithreaded environment, and without additional synchronization overhead, etc.

Important points about final

  1. The final keyword can be used for member variables, local variables, methods, and classes.
  2. Final member variables must be initialized at the time of declaration or in the constructor; otherwise, a compilation error will occur.
  3. You cannot reassign a value to a final variable.
  4. Local variables must be assigned a value at the time of declaration.
  5. All variables in an anonymous class must be final variables.
  6. Final methods cannot be overridden.
  7. Final classes cannot be inherited.
  8. The final keyword is different from the finally keyword, which is used for exception handling.
  9. The final keyword is easy to confuse with the finalize() method, which is defined in the Object class and is called by the JVM before garbage collection.
  10. All variables declared in an interface are final by default.
  11. The final and abstract keywords are inversely related; a final class cannot be abstract.
  12. Final methods are bound at the compilation stage, known as static binding.
  13. Final variables that are not initialized at the time of declaration are called blank final variables (blank final variable), and they must be initialized in the constructor or by calling this(). If not, the compiler will report an error: "The final variable (variable name) needs to be initialized."
  14. Declaring classes, methods, and variables as final can improve performance, allowing the JVM to make an estimate and then optimize.
  15. According to the Java code convention, final variables are constants, and usually the names of constants are capitalized:
private final int COUNT = 10;

Declaring a collection object as final means that the reference cannot be changed, but you can add, delete, or change the content within it. For example:

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 final variables, final methods, and final classes are. Using final when necessary can write faster and better code.

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

You May Also Like