Java Basic Series--final keywords

Source: Internet
Author: User
Tags java keywords

Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/8482909.html

I. Overview

Final is one of the most common Java keywords, meaning "final, immutable," in Java.

Content that is final modified will be different, they will become the ultimate existence, the content becomes a fixed existence.

Second, the role

2.1 Final modifier variable

The final modification of a variable becomes a constant, and the constant is saved in the method area.

Once a variable is final decorated, it must be initialized manually, and final constants that are not initialized cannot be compiled.

If only the initialization of a final modified variable can take:

--Assign value when defining

--code block assignment

--constructor assignment

If the initialization of a variable that is modified by both static and final can be used:

--Assign value when defining

--Static code block assignment

Once the final variable is modified by static, it is detached from the organization of the object (the code block, the constructor is the organization of the object), and is promoted to the organization of the class, so it needs to be initialized in a static code block at the class level.

1  Public class finaltest {2     Final int i = 1; 3     int j = 2; 4     Static int m = 3; 5     Static Final int n = 4; 6 }

Or

1  Public classFinaltest {2     Final inti;3     intJ;4     Static intm;5     Static Final intN;6     {7i = 1;8     }9     Static {Tenn = 3; One     } A}

If you change the code above to:

1  Public class finaltest {2     Final int i; 3     int J; 4     Static int m; 5     Static Final int N; 6 }

The above code, line 2nd and 5th, is an error because it is not initialized.

Then we summarize the final and static phenomena, which are used to differentiate between the two:

    The static modifier makes the content out of the object as a class member.

    The final modifier transforms the content into a member that must be manually initialized, and once assigned, no longer changes.

The two can exist at the same time, each play a role.

2.2 Final Modification method

The final modified method, can be quilt class inheritance, but not quilt class rewrite, that is, the method after which the internal implementation is fixed, can not be changed.

2.3 Final Modifier class

The final class, called the final class, no longer has subclasses and cannot be extended, the most common final classes are the string classes.

After the string class is final decorated, each of its objects is immutable, and once defined, it is no longer changed.

2.4 Final modifier Local variables

The final modified local variable, if the parameter of the method is final decorated, then the value of this parameter can not be changed from the value of the method call, and cannot be re-assigned (cannot be changed to his value).

The most common place is when the local inner class accesses the local variables of the method, these local variables need to use the final decoration, because the internal class accesses the local variables, the local variables are magnified, the local variables are generally invalidated at the end of the method, However, it is possible that the objects in the inner class are still in use. After the local variable is defined as final, it is no longer stored in the stack space, but is saved in the method area, and is naturally not lost because of the end of the method.

 1  public  void   Outmethod () { 2  final  int  s = 1;  3  class   innerclass{ 4  public  void   Innermethod () { 5   System.out.println (s);  6  }  7 }  8 } 

If the final of line 2nd is removed, the 5th line will be an error.

Java Basic Series--final keywords

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.