Final of Java Review

Source: Internet
Author: User

Final keyword
Used to represent the class that it modifies, methods, variables that cannot be changed
When a variable is modified, it cannot be changed once it gets its initial value, it can either modify the member variable or modify the local scalar, formal parameter, etc.
Final-Modified member variables must be realistic to specify initial values, and the system will not implicitly initialize the diet.

For example

public class Finalvariabletest {public   void Test (final int a)   {   //a=5;//error, should be initialized by the parameters passed in by the system, so this cannot be assigned   }public static void Main (string[] args) {final String str= "ZX";//define local variable default value, you can no longer be assigned//str= "12";//error, no longer assigned to final int a;//does not specify a default value that can be assigned once a=5;//a=4;//is assigned again, error}}

  

The difference between the basic type variable and the reference type variable of the final decoration
For a reference type variable, he saves only a reference, and final only guarantees that the referenced address is unchanged, that is, the same object is referenced
But the object can be arbitrarily changed.
For example

Class Personclass {private int age;public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Personclass () {}public-personclass (int age) {this.age=age;}}  Class Go {public static void main (string[] args) {final personclass pclass=new personclass;//pclass=null;//reference is no longer the original object, so there is an error  }}

  

The final method indicates that you do not want subclasses to override a method of the parent class
Like what

Class BaseClass {public final void get () {  } private final void set () {  }} class Sonclass extends BaseClass {// public void Get ()//error, cannot override final method public void set () {  }//because the private subclass cannot be accessed, it is equivalent to creating a new method}

There are immutable classes, such as the occurrence of the occasion, such as your project to initialize some immutable parameters, such as you use a map to save all the configuration files in the project key-value pairs, these key-value pairs are immutable, in order to prevent customer changes, you may need to use immutable classes, each returned to the user a protective copy.

As long as it is not a fixed, never change things, for the design of the class do not need immutable classes, the implementation of immutable classes also need to consume a certain amount of memory and performance, can be implemented by caching immutable classes to reduce overhead, concrete implementation methods, to write

Final of Java Review

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.