Final summary in Java

Source: Internet
Author: User

The Java keyword final has a final, immutable meaning that modifies non-abstract classes, non-abstract class member methods, and variables.

Error: Class "Testfinal" is either abstract, or final, not both.

1.final decorated classes are the ultimate. You cannot inherit from another class, there is no subclass.

2. Because the final class has no subclasses, the methods in the final class cannot be overwritten, so the

All methods and variables are final, and there is no need to explicitly declare a method final.

Public Final classtestfinal{ Public Static Final intI=1;//the data members in the final class can be final    protected intJ//the data members in the final class may not be final     Public voidF () {}//the method in the final class is final and may not be explicitly declared     Public Final voidg () {}//the methods in the final class can be declared final, but it doesn't make sense .}

3.The final method cannot be overridden by a method of a class, but can be inherited.

 Public class extends finalonmethod{    publicstaticvoid  main (string[] args) {        testfinal Tfinal=new  testfinal ();        Tfinal.one ();    }} class finalonmethod{    publicfinalvoid One () {        System.out.println ("Hello, I am a final method, but I am in a class that has no final modification oh!!!!!!!!!!!!!" );    }}

Let's rewrite how this is done by the final modification.

The error is at compile time.

Thus we associate the private method with the non-inheritance.

Because the system considers private members as final. We can also represent a private member as FIANL. But it doesn't make any sense.

4. The final member variable represents a constant, can only be assigned once, the value is no longer changed after the assignment, and a compilation error occurs if forced assignment.

5.final cannot be used to modify the construction method. The constructor method does not exist, it cannot be overridden, that is, the construction method itself is final, and there is no need to use the final label.

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