Java final summary and javafinal Summary

Source: Internet
Author: User

Java final summary and javafinal Summary

The Java keyword final has a final and unchangeable meaning. It can modify non-abstract classes, non-abstract class member methods, and variables.

Error: the class "TestFinal" is either abstract or final, not both.

1. The final modified class is the ultimate class. If it cannot be inherited by other classes, it cannot be a subclass.

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

All methods and variables are final and do not need to be explicitly declared as final.

 

Public final class TestFinal {public static final int I = 1; // The data member in the final class can be the final protected int j; // The data member in the final class may not be the final public void f () {}// the method in the final class is final, and public final void g () may not be explicitly declared () {} // methods in the final class can be declared as final, but it makes no sense}

3. The final method cannot be overwritten by the quilt class, but can be inherited.

 

Public class TestFinal extends FinalOnMethod {public static void main (String [] args) {TestFinal tFinal = new TestFinal (); tFinal. one () ;}} class FinalOnMethod {public final void One () {System. out. println ("hello, I am a final method, but my class is not decorated with final !!!!!!!!!!!!! ");}}

 

Next we will rewrite the final modified method.

An error is reported during compilation.

Therefore, we think that private methods cannot be inherited.

Because the system treats Private Members as final, we can also express a private member as fianl, but this is meaningless.

4. the final member variable indicates a constant and can only be assigned once. The value is not changed after the value is assigned. If the value is forcibly assigned, a compilation error occurs.

5. final cannot be used to modify the constructor. If a constructor is not inherited, it cannot be overwritten. That is to say, the constructor itself is final and there is no need to use final labels.

 

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.