The final keyword in Java

Source: Internet
Author: User

I. Final in Java can be used in data, methods, classes

1.FIANL Decorated data: Constants

When using the final keyword in front of a variable definition, the variable must be initialized at the time of definition, as follows:

Private Final Static int static_num1=10; // final constants must be created to complete initialization

constants must be initialized when they are created

When final and static are used together, you need to be aware of an initialization problem, as follows:

Here, using a non-static variable ran to initialize a static constant static_num2, an error occurred, not a non-static variable cannot initialize the static variable, but in the definition of the class variable precedence over the initialization of non-static variables, so in the creation of STATIC_ Num2 the RAN variable was not created and an error occurred.

to act as a formal parameter in a function, note the point:

 Public int Add (finalint  num) {        return num+1;   Num+1 is able to return   but num++ and ++num cannot be returned because NUM is not a non-editable value, the prefix increment or suffix increment will change its own value, but using +1 just as a constant    }

2.final Decorated Object references

Note: Fianl decorated objects are not objects that cannot be changed, but are decorated by references that cannot be pointed to other objects , and the object itself can be modified.

Examples are as follows:

Then, when passing an argument to a function, the final modified object is important, and he can guarantee that your reference argument will not be modified in the function. Look at the following example:

 Public Static void F (final  a a) {        System.out.println ("parameter called fianl type");         New A ();//Error    }

When we call this function f, the arguments passed in to the Class A reference do not need to be of type fianl.

3.final Retouching Method:

The final modification method is usually to make a method cannot be inherited when the quilt class is overridden, then using the keyword private is the same effect, he will make the subclass can not get the parent class of the method.

Example:

Parent class:

Sub-class:

For F1 () that uses the final decoration, the compiler directly reports an error that cannot be overridden. For the F2 () method that uses the private modifier, the compiler directly reports a hint to remove the rewrite label (@override), which means that when the tag is removed, it is equivalent to re-establishing a new method in the subclass F2 (), which is not related to the parent class in any way.

4.final-Decorated classes

Using FIANL to decorate a class, the class would be impossible to inherit, and if the class could not be inherited, there would be no method rewrite, so for a final class his method would be the default final.

Example:

The final keyword in Java

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.