Java final keywords

Source: Internet
Author: User

1. The final class cannot be inherited, and there is no subclass. The methods in the final class are final by default.
2. The final method cannot be overwritten by the quilt class, but can be inherited.

3. Final member variables indicate constants and can only be assigned once. The values do not change after being assigned values.

4. Final cannot be used to modify the constructor.

Note: The Private member method of the parent class cannot be overwritten by the quilt class method. Therefore, the private type method is of the final type by default.

Public class finalex {public static void main (string ARGs []) {final Student stu1 = new student (22, "Tom"); Student stu2 = new student (25, "Jerry"); // stu1 = stu2; // error. stu1 points to the new student (22, "Tom") object and cannot change the system. out. println ("stu1 name:" + stu1.sname + "stu1 age:" + stu1.sage); stu1.setsname ("Jerry"); stu1.setsage (25); system. out. println ("stu1 name:" + stu1.sname + "stu1 age:" + stu1.sage); // This is the object pointed to by stu1}
} Class student {string sname; int Sage; Public student (INT sage, string sname) {This. sage = Sage; this. sname = sname;} public void setsname (string sname) {This. sname = sname;} public void setsage (int sage) {This. sage = Sage ;}}

Where:

For the basic type, final keeps the value constant.
For object reference, final makes the reference constant, but the content pointed to by the reference can be changed.

2. When the method parameter is modified to final, you can read and use the parameter, but cannot change the parameter (the object pointed to by the parameter reference), for example:

int test(final int i){    //i++; error    return i+1;}

3. If a blank final is declared, initialization must be performed in the constructor of the class. Otherwise, the compiler reports an error:

public class Test {    final int j;    Test(){        j=1;    }}

4. When a class is declared as final, the class will not be inherited, and the methods in the class are also final by default.
When a method is declared as final, the method will not be overwritten.

However, if you try to override a private method inherited from the parent class, the compiler will not report an error,

Only the method of the parent class private is not part of the parent class interface. At this time, you did not overwrite this method, but generated a new method.

public class Test {    private final void t(int i){}}public class Test2 extends Test {    public void t(int i) {        System.out.println("t");    }    public static void main(String[] args) {        Test t = new Test2();    }}

The T method in Test2 is just the same as the name in the parent class.
Only when the T method in the parent class is public and protected (not final at the same time) Can the T method in test2.

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.