Java Final keyword

Source: Internet
Author: User

---restore content starts---

Definition of final keyword

Final: Ultimate, immutable. is a modifier that modifies a class, modifies a member of a class, a local variable, and cannot modify a construction method

The final feature

The final decorated class cannot be inherited, but can be inherited from other classes, not inherited (Eunuch Class)

1  Public Final class Fu {     parent class 2public     void  A () {3         System.out.println ("a"); 4     }5 }
1  Public class extends fu {      // because the parent class is the final decoration, all cannot be inherited 2  3 }

Final decorated methods can not be overridden by a class, the parent method decorates the final is dead, subclass inheritance can only override the normal method, and can be added final

1  Public class Fu {2      Public void A () {      3         System.out.println ("a"); 4     }5public     void  B () {6         System.out.println ("B"); 7     }8 }
1  Public classZiextendsFu{Subclass inherits the parent class and overrides the method2      Public Final voidA () {3System.out.println ("C");4     }5      Public voidB () {6System.out.println ("D");7     }        8}

Final definition is always bright, can only be assigned once, never change

final int a = 2; Multiple ways of assigning values

Final int   A;      Public Fu () {        a=2;    }

Java Final keyword

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.