Java Learning (vi) Object-oriented final keyword

Source: Internet
Author: User

1. Methods that are modified by fnial cannot be rewritten, and are commonly used for cosmetic classes, methods, variables

/*final can modify the class, method, variable characteristics: Final can be decorated class, the class cannot be inherited. Final can modify the method, and the method cannot be overridden. (Overwrite, duplicate) Final can modify the variable, and the variable cannot be re-assigned.            Because this variable is actually constant. Constant: A: literal constant "Hello", 10,true B: custom constant Final int x = ten;*///Final class Fu//unable to inherit from final FuclassFu { Public intnum = 10;  Public Final intnum2 = 20; /*Public final void Show () {}*/}classZiextendsFu {//Show ( ) in Zi cannot overwrite show () in Fu     Public voidShow () {num= 100;                SYSTEM.OUT.PRINTLN (num); //cannot assign a value to the final variable num2//num2 = $;System.out.println (num2); }}classFinaldemo { Public Static voidMain (string[] args) {Zi Z=NewZi ();    Z.show (); }}

Java Learning (vi) Object-oriented 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.