The meaning of final in Java

Source: Internet
Author: User

Http://zhidao.baidu.com/question/397935417.html
1.final decorated classes cannot be inherited and have no subclasses.
"Use environment":
1. Classes that are not specifically designed for inheritance have complex invocation relationships between the methods of the class itself. If you arbitrarily create subclasses of these classes, subclasses may incorrectly modify the implementation details of the parent class
2. For security reasons, the implementation details of the class are not allowed to change
3. When creating the object model, be sure that the class will no longer be extended
1. If a data is both static and final, it will have a storage space that cannot be changed.

2. Final data: When final is used for the base data type, final lets its value remain the same, but when used with object reference, final only keeps reference intact. This means that when reference is initialized to represent an object, it can no longer be changed to point to another object, but the content of the object itself does change. Final has the same effect on array as it does for reference. Refer to the following example:

public class test1{
private final int li_int=12;
Private final Inclass inclass1=new Inclass (5);
Private final Inclass inclass2=new Inclass (8);
public void modifiedfinal (int a) {
The following statement has a compilation error and cannot modify the value of the final base type
Li_int = A;
The following statement has a compilation error and cannot point the initialized final variable to another object
Inclass1=inclass2;
The following statement succeeds, although the reference cannot be changed but the contents of the object itself referenced by the final variable can be changed
Inclass1.mod (a);
}
Class inclass{
int li_a=0;
Public Inclass (int a) {
Li_a=a;
}
public int mod (int b) {
Li_a=b;
return li_a;
}
}
public static void Main (String args[]) {
Test1 test1=new Test1 ();
Test1.modifiedfinal (100);
System.out.println (TEST1.INCLASS1.LI_A);
}
}

3. Blank Finals:java allows data members to be declared final without assigning an initial value. However, the blank finals must be initialized before use and must be initialized in the constructor. Please refer to the following example:

public class test2{
The final variable is initially allowed to be unassigned
private final int li_int;
Public Test2 (int a) {
The following statement compiles through, the assignment of a final variable that is defined as null must be done in the constructor method, and must be assigned, without assigning a value and error
Li_int = A;
}
public int mod (int a) {
The following statement compiles an error, and the assignment of a final variable that is defined as null must be done in the constructor method
Li_int = A;
return li_int;
}
}

4, Final arguments: the declaration of arguments as final, you can ensure that the argument cannot be pointed to it, when Argment is the basic data type, it means that the value can not be changed. Refer to the following example:

public class test3{
private int li_int=12;
Private Inclass inclass1=new Inclass (5);
Private Inclass inclass2=new Inclass (8);
public void modifiedfinal (final int a,final inclass in) {
The following statement has a compilation error and cannot modify the value of the final base type
A = 15;
The following statement has a compilation error and cannot point the initialized final variable to another object
In=inclass2;
The following statement succeeds, although the reference cannot be changed but the contents of the object itself referenced by the final variable can be changed
In.mod (a);
}
Class inclass{
int li_a=0;
Public Inclass (int a) {
Li_a=a;
}
public int mod (int b) {
Li_a=b;
return li_a;
}
}
public static void Main (String args[]) {
int a=100;
Internal class initialization
Test3 test3=new Test3 ();
Test3.inclass in=test3.new Inclass (30);
System.out.println (IN.LI_A);
Test3.modifiedfinal (A,in);
System.out.println (IN.LI_A);
}
}

5, Final methods: You can lock the method, do not let the inheriting class change its meaning (do not allow subclasses to overwrite); allow the compiler to call this method as an inline method. Refer to the following example:

public class test4{
private final int li_int=0;
Public final int Pub_fi_mod () {
return li_int;
}
Protected final int pro_fi_mod () {
return li_int;
}
Private final int pri_fi_mod () {
return li_int;
}
private int Pri_mod () {
return li_int;
}
}

public class Test5 extends test4{
private int li_i=100;
The following method compiles an error, cannot overwrite the final method, only for public and protected, the method in the subclass is the same as the method name in the parent class, not overwrite, and there is no relation to the method name in the parent class (except for the same name).
/*
public int Pub_fi_mod () {
return li_i;
}
protected int Pro_fi_mod () {
return li_i;
}*/
Private final int pri_fi_mod () {
return li_i;
}
private int Pri_mod () {
return li_i;
}
public static void Main (String args[]) {
System.out.println (New Test5 (). Pri_mod ());
}
}

6, Fianl (method) vs Private (method): Class all of the private methods is naturally final,private methods is just a piece of code hidden in the class, Cannot be overrid, even if there is a method of the same name in the subclass, it does not produce any effect; the difference between the two is that the methods with the same signature as the private method can appear in the subclass, and the final method of public or protected cannot be overridden. However, refactoring methods that allow the same method name but different argument lists appear. Using the above example, the TEST5 is modified and compiled by:

public class Test5 extends test4{
private int li_i=100;
The following method compiles an error and cannot overwrite the final method
/*
public int Pub_fi_mod () {
return li_i;
}
protected int Pro_fi_mod () {
return li_i;
}
*/
But the refactoring method that allows different argument lists to appear
public int Pub_fi_mod (int a) {
return li_i;
}
protected int Pro_fi_mod (int a) {
return li_i;
}
Private final int pri_fi_mod () {
return li_i;
}
private int Pri_mod () {
return li_i;
}
public static void Main (String args[]) {
System.out.println (New Test5 (). Pri_mod ());
}
}

7, Final classes: When a class is declared final, it also determines that the class will not be inherited (such as the String class, which is the final class, see its implementation java.lang.String). Final classes can be final or non-final, the data members can be final or not, they will obey the principle of final data. Refer to the following example:

Public final class test6{
private final int li_int=0;
public int li_a=123;
Public final int mod () {
return li_int;
}
public int Pri_mod () {
return li_a;
}
public static void Main (String args[]) {
System.out.println (New Test6 (). Pri_mod ());
}
}

Test6 is the final class, so Test7 cannot inherit
public class Test7 extends test6{
private int li_int=0;
}



PS: As can be seen from the above, final is the address of an object unchanged, the value of the base type remains the same (because the base type variable points to the physical address holding value and the object variable points to the physical address of the object contents of the address).

PS: Before reading the teacher said in Java final definition constants, only half, to the basic type is right, and string is also right, because string, although the object, But there is no case where the string variable address is constant and its contents change (string is a whole cannot only change one of the characters), so it is correct, but the other object can only keep its reference address unchanged, it is not guaranteed that its contents are not changed, so it is wrong.

Add some more content:

1, the final attribute is assigned at the time of Declaration, and the assigned value is a constant, the compiler will use all the places used in this property is replaced by constants, this is referred to the following code:

Package com.xx.dryr.test1;

Import Java.lang.reflect.Field;

public class test1class1{

public final int x = 100;

public int F (test1class1 T1c11,test1class1 t1c12) throws exception{

int i = t1c11.x;

System.out.println ("I ' s value is" +i);

Changex (T1C11);

int j = t1c12.x;

System.out.println ("J ' s value is" +j);

return j-i;

}

public static void Changex (Test1class1 t1c1) throws exception{

Class clazz = T1c1.getclass ();

Field FIELDX = Clazz.getdeclaredfield ("x");

Fieldx.setaccessible (TRUE);

Fieldx.setint (T1C1, 300);

System.out.println ("Fieldx s Vlaue is" +fieldx.getint (T1C1));

}

public int test () throws exception{

return f (this,this);

}

public static void Main (string[] args) throws exception{

Test1class1 t1c1 = new Test1class1 ();

System.out.println (T1c1.test ());

}

}

The operating result is:

I ' s value is 100

Fieldx ' s Vlaue is 300

J ' s value is 100

0

Although the value of x has been modified to 300 in the Changex method, the value of X at run time will not affect where x is used, since all the uses of X at compile time are replaced with 100.

2, otherwise, the value of the final attribute can be changed if the final attribute value is not determined at compile time. Please refer to the code below to make a slight modification to the above code so that the final attribute x is initialized in the constructor method:

Package com.xx.dryr.test1;

Import Java.lang.reflect.Field;

public class test1class1{

public final int x;

Public Test1class1 () {

x = 100;

}

public int F (test1class1 T1c11,test1class1 t1c12) throws exception{

int i = t1c11.x;

System.out.println ("I ' s value is" +i);

Changex (T1C11);

int j = t1c12.x;

System.out.println ("J ' s value is" +j);

return j-i;

}

public static void Changex (Test1class1 t1c1) throws exception{

Class clazz = T1c1.getclass ();

Field FIELDX = Clazz.getdeclaredfield ("x");

Fieldx.setaccessible (TRUE);

Fieldx.setint (T1C1, 300);

System.out.println ("Fieldx s Vlaue is" +fieldx.getint (T1C1));

}

public int test () throws exception{

return f (this,this);

}

public static void Main (string[] args) throws exception{

Test1class1 t1c1 = new Test1class1 ();

System.out.println (T1c1.test ());

}

}

The operating result is:

I ' s value is 100

Fieldx ' s Vlaue is 300

J ' s value is 300

200

As seen from the above example, the value of the final attribute can be changed, but only in special cases (not replaced at compile time), the value of the final property can be changed in a special way (like reflection). So it is generally said that the value of the final attribute is not allowed to be modified or can be said, but must be aware of these exceptions.

The meaning of final in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.