The final modifier and Javafinal modifier in Java

Source: Internet
Author: User

The final modifier and Javafinal modifier in Java

1. When can I select the final modifier?

If you want to prevent a class from being inherited by other classes and are not allowed to have child classes, you should consider using final for modification.

2. Classes modified with final

First, you must understand that the class modified with final cannot be inherited. Let's look at an error case.

Eg:

final class Penguin{}class SubPenguin extends Penguin{}

In this case, the class SubPenguin extends Penguin line of code will report an error:

The type SubPenguin cannot subclass the final class

That is, the. Penguin class cannot be inherited.

3. final modification method

Eg:

Class Penguin {public final void print () {}} class Subpenguin extends Penguin {public void print () {}// error}

In this case, the public void print () {} code returns an error with the error message:

Cannot override the final method from Penguin

That is, the. print method cannot be overwritten by the quilt class.

4. Variables modified with final

Variables modified with final include member variables and local variables. They will become constants and can only be assigned once.

Public class Dog {final String name = "Xiao Huang"; // use final to modify the Dog's name public void setname (String name) {this. name = name; // error. name cannot be assigned .}}

5. Notes for using the final Modifier

Final can be used to modify classes, methods, and attributes. It cannot modify constructor methods.

6. Can the attribute values of objects referred to by variables be changed in referenced variables modified with final?

First, let's look at a simple example.

// Dog class public class Dog {String name; // name of the Dog // with parameters. Assign the Dog name public Dog (String name) {this. name = name ;}// Test class Test {public static void main (String [] args) {final Dog = new dog ("Xiao Huang "); // instantiate a dog object and use final to modify the dog. name = "rhubarb"; dog = new Dog (" ");}}

In this Code, the dog object is modified to final as a constant, and its value is immutable, but it should be noted that it is dog. name = "rhubarb"; this line of code is not wrong, so dog = new Dog ("small eight") is wrong, that is, the reference variable modified with final, A variable cannot point to another object, but its content can be changed.

Conclusion: When Using final modified referenced variables, the value of the variables remains unchanged, and the attribute values of the objects referred to by the variables are variable.

 

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.