Java Object-oriented programming (IX)--final

Source: Internet
Author: User

1. Final concept

Final can modify variables or methods. In some cases, the programmer may have the following requirements:

A, when you do not want a method of the parent class to overwrite (override) The quilt class, you can use the final keyword to decorate.

b, when you do not want the value of a variable of a class to be modified, you can use the final decoration. If a variable is final, the initial value must be assigned, or a compilation error occurs.

C, when you do not want the class to be inherited, you can use the final decoration .

//use of the final method [Demo128.java] Public classDemo128 { Public Static voidMain (string[] args) {AAA AAA=NewAaa ();        Aaa.show (); BBB BBB=NewBBB ();    Bbb.show (); }}classaaa{inta=0;//If a does not assign an initial value, a is 0. Value should be assigned after defining the type//PI does not allow modification//a variable with a final decoration should be named with an underscore to differentiate the representation. This is the standard for Java programmers.     Final floatreate_1=3.1415926f;//use final to ensure that data that needs to be forced to be unmodified must be locked with final//final int b; //when you use final to define a variable, you must assign an initial value or an error. //b=1;    Final  Public voidSendmes () {//using final to modify a member method means that it cannot be modified and cannot be overwritten. SYSTEM.OUT.PRINTLN ("Send Message"); }     Public voidShow () {System.out.println ("A=" +a); }}Final classBbbextendsaaa{//defining a class before and final means that the class is not allowed to be inherited     PublicBbb () {a++; //reate_1=reate+1;    }    /*Public void Sendmes () {System.out.println ("Send Message")}*/}

final-- Precautions

1. The final modified variable is also called constant, which is usually named by Xx_xx_xx. (underlined)

2. When the final modified variable is defined, it must be assigned and cannot be assigned later.

final-- when do you use

1, because of case considerations, a method of the class does not allow modification.

2. Classes are not inherited by other classes.

3, some variable values are fixed, such as Pi 3.1415926

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.