Fianl modifier of Java, javafianl Modifier

Source: Internet
Author: User

Fianl modifier of Java, javafianl Modifier

The variable modified by fianl cannot be changed. Once the initial value is obtained, the final variable value cannot be re-assigned.

1. final member variable (the programmer must explicitly specify the initial value)

Class variable: the initial value must be specified in the static initialization block or when the variable is declared.

Instance variable: You must specify the initial value in a non-static initialization block, declare the variable, or in the constructor.

2. final local variables

The local variables defined by final can be defined by default or not. If the default value is not specified during definition, you can specify the default value in future code, but only once.

3. Differences between final modification of basic type variables and reference type variables

When final modifies a basic type variable, you cannot assign a value to the variable of the basic type object. However, when final modifies a reference type variable, it only saves a reference. final only ensures that the reference address of this application type variable remains unchanged, that is, it always references the same object, but this object can be completely changed.

Public class Test () {public static void main (String [] args) {// final modifier array variable, is a reference variable final int [] iArr = {5, 6, 7, 8 }; // output 5 6 7 8 System. out. println (Arrays. toString (iArr); // assign values to the array elements, valid iArr [2] =-8; // output 5-8 7 8 System. out. println (Arrays. toString (iArr ));}}

4. final Method
The final modifier method cannot be overwritten.

5. final class

The classes modified by final cannot have child classes and cannot be inherited.

 

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.