java-final keywords

Source: Internet
Author: User

Final: Finally, that's it.

In three different situations:

1. Decoration class

Indicates that the class cannot be inherited and extended

Final class A {} class extends A {}//  Compiler error indicating that it cannot inherit

2. Modification method

Subclasses cannot overwrite and override this method

class a {    finalvoid  method () {}}classextends  A {    @ Override    void method () {}//  compiler Error, unable to override it }

3. Modifier variables

For an object, the reference to the variable is guaranteed to be unchanged and its value can be modified

For basic type data, the guaranteed value is the same. Same as when modifying method parameters

 Public classFinaldemo { Public Static voidMain (string[] args) {String a= "Hello2"; FinalString B = "Hello"; String C= B + 2;        System.out.println (A.equals (c)); String D= "Hello"; String e= d + 2;                System.out.println (A.equals (e)); Modify the value of the current objectFinalStringBuffer Tempbuffer =NewStringBuffer ("Hello, world!"); Tempbuffer.append ("This is final string!"); //Tempbuffer = new StringBuffer ();//Unable to re-reference data    }}

java-final keywords

Related Article

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.