Deep understanding of final keyword in Java

The final keyword in java is very important and can be applied to classes, methods and variables. What is it to declare variables, methods, and classes as final tables?

What are the benefits? Use final and static together to declare a constant.

final constant

Any member variables or local variables (variables in 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:

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

final variables are read-only.

What is a final method?

final can also declare methods. The final keyword in front of a method means that the method cannot be overridden by methods of subclasses. If you think a method is functional enough that it doesn't need to be changed in subclasses, you can declare the method 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:

1
2
3
4
5
6
7
8
9
10
11
12
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 modified with final is called a final class. Final classes are usually functionally complete, they cannot be inherited. There are many classes in Java that are final, such as String, Interger and other wrapper classes. The following is an example of a final class:

1
2
3
4
5
6
7
     final class PersonalLoan{
 
     }
 
     class CheapPersonalLoan extends PersonalLoan{  //compilation error: cannot inherit from final class
 
}


The benefits of the final keyword

Some of the benefits of using the final keyword are summarized below

  1. The final keyword improves performance. Both the JVM and Java applications 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 optimizes methods, variables and classes.

Important knowledge 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 be reported.
  3. You cannot assign a final variable again.
  4. Local variables must be assigned a value when they are declared.
  5. All variables in an anonymous class must be final.
  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. final关键字容易与finalize()方法搞混,后者是在Object类中定义的方法,是在垃圾回收之前被JVM调用的方法。
  10. 接口中声明的所有变量本身是final的。
  11. final和abstract这两个关键字是反相关的,final类就不可能是abstract的。
  12. final方法在编译阶段绑定,称为静态绑定(static binding)。
  13. 没有在声明时初始化final变量的称为空白final变量(blank final variable),它们必须在构造器中初始化,或者调用this()初始化。不这么做的话,编译器会报错“final变量(变量名)需要进行初始化”。
  14. 将类、方法、变量声明为final能够提高性能,这样JVM就有机会进行估计,然后优化。
  15. 按照Java代码惯例,final变量就是常量,而且通常常量名要大写:
1
private final int COUNT = 10 ;
  1. 对于集合对象声明为final指的是引用不能被更改,但是你可以向其中增加,删除或者改变内容。譬如:
1
2
3
4
private final List Loans = new ArrayList();
list.add(“home loan”);  //valid
list.add( "personal loan" ); //valid
loans = new Vector();  //not valid

我们已经知道final变量、final方法以及final类是什么了。必要的时候使用final,能写出更快、更好的代码的。








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644547&siteId=291194637