1. final variable
Final variable is a constant that cannot be changed once it is initialized.
class Test1 { final double PI = 3.14; //The name of the constant is best capitalized public Test1(){ PI = 3.14; } void test(){ System.out.println("PI is: " + PI); } public static void main(String[] args){ Test1 t = new Test1(); t.test(); }} Output:
PI is: 3.14
(1) Blank final variable
The final variable that is not initialized at the time of declaration is called blank final variable. The blank final variable must be initialized in the constructor, otherwise a compilation error will be thrown.
class Test1 { final double PI; Test1(){ PI = 3.14; //Initialize in the constructor} void test(){ System.out.println("PI is: " + PI); } public static void main(String[] args){ Test1 t = new Test1(); t.test(); }} Output:
PI is: 3.14
(2) Uninitialized static final variable
The static final variable that is not initialized during the declaration phase can only be initialized in static code blocks.
class Test1 { static final double PI; static { PI = 3.14; } void test(){ System.out.println("PI is: " + PI); } public static void main(String[] args){ Test1 t = new Test1(); t.test(); }} Output:
PI is: 3.14
2. final method
The final method cannot be overwritten. That is to say, a subclass can call the fianl method of the parent class, but cannot overwrite it.
class Test { static final double PI = 3.14; final void test(){ System.out.println("PI is: " + PI); }}class Test1 extends Test{ public static void main(String[] args){ Test1 t = new Test1(); t.test(); }} Output:
PI is: 3.14
3. final class
final cals cannot be inherited
final class Test1 { static final double PI = 3.14; final void test(){ System.out.println("PI is: " + PI); } public static void main(String[] args){ Test1 t = new Test1(); t.test(); }} Output:
PI is: 3.14
PS: Why is the java.lang.String class designed as final?
First of all, you must be clear about the keyword final.
Final appears to not want to change, but there are two reasons for not wanting to change: design or efficiency. The class modified by final cannot be inherited, so the class modified by final cannot be tampered with.
After understanding this, let's take a look at the problem:
(1) From a design perspective,
A. Make sure they do not change semantics in subclasses. The String class is a final class, which means that no one is allowed to define a subclass of a String. In other words, if there is a String reference, it must refer to a String object, but it cannot be an object of other classes.
B. String cannot be modified once it is created, because the java designer will share the String. The following paragraph is a comment in the source code:
(2) From the perspective of efficiency:
A. Designed as final, the JVM does not need to query related methods in the virtual function table, but directly locates them on the related methods of the String class, improving execution efficiency.
B. Java designers believe that sharing brings more efficiency.
In short, it is necessary to ensure that the object referenced by java.lang.String must be an object of java.lang.String, not a class that references its descendants, so as to ensure its efficiency and safety.