Final
According to the program context, the Java keyword final has "This is immutable" or "final State" meaning, it can modify the Non-abstract class, Non-abstract class member methods and variables. You may need to stop the change in terms of two understandings: design or efficiency.
The final class cannot be inherited, no subclasses, and the method in the final class is final by default.
The final method cannot be overridden by a quilt-class method, but can be inherited.
Final member variables represent constants that can only be assigned once, and values are not changed after assignment.
Final cannot be used to modify the construction method.
Note: The private member method of a parent class cannot be overridden by a quilt class method, so the private type method defaults to the final type.
1. Final class
The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. When designing a class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is assured that the class will not be extended, then the final class is designed.
2. Final method
If a class does not allow its subclasses to overwrite a method, you can declare the method as the final method.
There are two reasons for using the final method:
First, the method is locked to prevent any inherited class from modifying its meaning and implementation.
Second, efficient. The compiler goes into the inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.
For example:
public class Test1 {public
static void Main (string[] args) {
//TODO automatically generate method stub
} public
void F1 () {
Sy Stem.out.println ("F1");
}
Methods that cannot be overridden by a quilt class are public
final void F2 () {
System.out.println ("F2");
}
public void F3 () {
System.out.println ("F3");
}
private void F4 () {
System.out.println ("F4");
}
public class Test2 extends Test1 {public
void F1 () {
System.out.println ("Test1 parent method F1 overwritten!");
public static void Main (string[] args) {
Test2 t=new Test2 ();
T.f1 ();
T.f2 (); Call the final method T.f3 () inherited from the parent class
();//Call the method inherited from the parent class
//t.f4 ();//call failed, cannot inherit from parent class
}
3, final variable (constant)
The final decorated member variable represents a constant, and the value cannot be changed once given.
There are three kinds of final modified variables: Static, instance, and local variables, representing three types of constants, respectively.
In addition, when the final variable is defined, it can be declared first, without giving the initial value, which is also called the final blank, and in any case the compiler ensures that blank final must be initialized before it is used. However, the final blank provides greater flexibility in the final keyword final use, for which the final data members of a class can achieve different objects, but retain their constant invariant characteristics.
Package org.leizhimin;
public class Test3 {private final String s= "final instance variable S";
private final int a=100;
public final int b=90;
public static final int c=80;
private static final int d=70; public final int E;
Final whitespace, the initial value must be assigned to the public Test3 (int x) {e=x) when the object is initialized;
}/** * @param args */public static void main (string[] args) {Test3 t=new Test3 (2); t.a=101; Error, the value of final variable can not change//t.b=91 once given; Error, the value of final variable can not change//t.c=81 once given; Error, the value of final variable can not change//t.d=71 once given;
Error, the value of the final variable cannot be changed System.out.println (T.A) once given;
System.out.println (T.B); System.out.println (T.C); Object-mode access to static fields is not recommended System.out.println (T.D);
Object-mode access to static fields is not recommended System.out.println (TEST3.C);
System.out.println (TEST3.D); System.out.println (TEST3.E);
Error, because E is final white, depending on different object values.
System.out.println (T.E);
Test3 t1=new Test3 (3); System.out.println (t1. E); Final whitespace variable e varies according to object} private void Test () {System.out.println (new Test3 (1).
A);
System.out.println (TEST3.C);
System.out.println (TEST3.D); } PUBlic void Test2 () {final int A;//final blank, assigned final int b=4 when needed;//local constant--final for local variables final int c;//final blank, never assigned
Value. a=3; a=4;
Error, the value has been assigned. b=2;
Error, the value has been assigned. }
}4. Final parameter
When a function argument is a final type, you can read it using this parameter, but you cannot change the value of the parameter.
public class Test4 {public
static void Main (string[] args) {
new Test4 (). F1 (2);
}
The public void F1 (final int i) {
//i++;//i is a final type and the value is not allowed to change.
System.out.print (i);
}