Final modifier in Java

Source: Internet
Author: User

1. When can I select the final modifier

If you want a class to not be inherited by another class, it is not allowed to have subclasses, so this is the time to consider final.

2. Final-Modified Classes

First of all, we must understand that the final modified class is not inherited , the following is a case of error.

eg

Final class penguin{} class extends penguin{}

At this point the Code class Subpenguin extends penguin this line of code will be error:

The type Subpenguin cannot subclass the final class: translation: The Type Subpenguin the last classes that cannot inherit

That is, the. Penguin class cannot be inherited.

3. The final modification method

eg

  class penguin{    publicfinalvoid class extends  penguin{   publicvoid print () {}// error }

At this point, the code public void print () {} will cause an error message:

Cannot override the final method from Penguin: translation: The last way to not cover penguins

That is, the. Print method cannot be overridden by a quilt class.

4. Final Modified variables

Final-modified variables include member variables and local variables, and they become constants that can be assigned only once.

 Public class  final String name= "small yellow"; // use final to decorate the dog's name  Public void SetName (String name) {   this. Name=name;    // error, name cannot be assigned to a value. }}

5. Note Points using the final modifier

Final can be used to decorate classes, methods, and properties, and cannot be decorated with construction methods.

6. Can the value of the object's property be changed with the final modified reference type variable?

Let's start with a simple example

//Dog Class Public classdog{String name;//the name of the dog//to assign a name to a dog, with a reference structure   PublicDog (String name) { This. name=name;} }//Test Class classtest{ Public Static voidMain (string[] args) {FinalDog dog=NewDog ("Xiao Huang");//instantiate a Dog object, with final decorationDog.name= "Rhubarb"; Dog=NewDog ("Hachi")); }}

In this code, the dog object is modified to be the final constant, the value is immutable, but note that dog.name= "rhubarb", the line of code is not wrong, then dog=new Dog ("Hachi") is wrong, that is, using the final modified reference variable, A variable cannot point to another object, but the contents of the variable you are referring to can be changed.

Conclusion: When you use a final modified reference variable, the value of the variable is fixed, and the value of the object that the variable refers to is variable.

Final modifier 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.