Use of the final modifier in Java

Source: Internet
Author: User

Use of 1.final modifiers:

    • Final can modify the variable, and after the final modified variable is assigned the initial value, it cannot be re-assigned.
    • Final can modify the method, and the final modified method cannot be overridden.
    • Final can modify the class, and the final decorated class cannot be inherited.

The above "grammar formulas" are still not enough to really master the final modifier.

2.final modified variable: The final modified instance variable must display the specified initial value, and the initial value can only be specified in the following three locations:

    • Specifies the initial value when defining a final instance variable.
    • Specifies the initial value for the final instance variable in a non-static initialization block.
    • Specify the initial value for the final instance variable in the construction method.

  

 PackageObjectstudy; Public classFinalinstancevaribaletest {Final intvar1 = 1;//Specifies the initial value when defining a final instance variable.     Final intvar2; Final intVar3; //Specifies the initial value for the final instance variable in a non-static initialization block. {var2= 2; }        //Specify the initial value for the final instance variable in the construction method.      Publicfinalinstancevaribaletest () { This. VAR3 = 3; }         Public Static voidMain (string[] args) {finalinstancevaribaletest finalinstancevaribaletest=Newfinalinstancevaribaletest ();        System.out.println (FINALINSTANCEVARIBALETEST.VAR1);        System.out.println (FINALINSTANCEVARIBALETEST.VAR2);    System.out.println (FINALINSTANCEVARIBALETEST.VAR3); }}

Use of the final modifier in Java

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.