Among the numerous keywords in the Java language, the final keyword is undoubtedly the most mentioned and also the knowledge point often asked in the interview process. Today, Lao Wang searched a lot of materials, and finally collected a complete knowledge point about the final keyword. First, the final keyword can modify three objects: variables, methods, and classes. Let’s take a look at the differences between the three types of objects modified by the final keyword.

1. The final keyword decorates variables

There are two types of variables that can be modified with the final keyword: variables of a base data type and variables of a reference type. The final keyword must assign an initial value to a variable when modifying basic data types. Therefore, final is often used to declare constant values with the static keyword.

// Define constants
final static String str = "Lao Wang said programming.";
Copy the code

When modifying a reference variable, it cannot be assigned if it has already been assigned, or it will fail to compile.

// Define and instantiate a Main object
final Main main = new Main();
// An error occurs when the Main object is reassigned after it has been modified with the final keyword
main = new Main();
Copy the code

2, final keyword modification method

Methods decorated with the final keyword cannot be overridden by subclass methods, which is equivalent to a method whose functionality is already defined and cannot be overridden. In general, if the function of a method has been determined and will not be changed, you can use the final keyword to modify it, because the method modified by the final keyword is dynamically bound at the time of compilation, rather than at the time of execution, which greatly improves the efficiency of execution.

public final void method(a) {
    System.out.println("it is final method");
  }
Copy the code

3. The final keyword decorates the class

Classes decorated with the final keyword are usually called final classes. Final classes cannot be inherited, which means that final classes are usually complete. As a result, there are many classes in the Jdk that are decorated with the final keyword and do not need to be inherited. Of these, the most common String class is the fianl keyword modified class, as well as other decorative classes and so on.

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {}public final class Integer extends Number implements Comparable<Integer> {}
public final class Long extends Number implements Comparable<Long> {}
Copy the code

The above and other decorator classes are all decorated with the final keyword, and their own methods and functionality are complete.

4, static keyword difference

The final keyword is used in pairs, and the final static keyword is used in pairs. Variables decorated static will only be initialized when the class is loaded and will not change when the object is created again.

// Define a variable num in the class to receive random numbers, since num is initialized only when the class object is loaded.
// Therefore, the value of num will not change after that
static double num = Math.random();
Copy the code

More exciting to wechat public number [Lao Wang said programming] >>>