Keywords in Java final

Source: Internet
Author: User
Tags abstract final variables

Final

According to the program context, the Java keyword final has "This is immutable" or "final State" meaning, it can modify the Non-abstract class, Non-abstract class member methods and variables. You may need to stop the change in terms of two understandings: design or efficiency.

The final class cannot be inherited, no subclasses, and the method in the final class is final by default.

The final method cannot be overridden by a quilt-class method, but can be inherited.

Final member variables represent constants that can only be assigned once, and values are not changed after assignment.

Final cannot be used to modify the construction method.

Note: The private member method of a parent class cannot be overridden by a quilt class method, so the private type method defaults to the final type.

1. Final class

The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. When designing a class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is assured that the class will not be extended, then the final class is designed.

2. Final method

If a class does not allow its subclasses to overwrite a method, you can declare the method as the final method.

There are two reasons for using the final method:

First, the method is locked to prevent any inherited class from modifying its meaning and implementation.

Second, efficient. The compiler goes into the inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.

For example:

public class Test1 {public   
      
static void Main (string[] args) {   
//TODO automatically generate method stub   
} public   
      
void F1 () {   
Sy Stem.out.println ("F1");   
}   
Methods that cannot be overridden by a quilt class are public   
final void F2 () {   
System.out.println ("F2");   
}   
      
public void F3 () {   
System.out.println ("F3");   
}   
      
private void F4 () {   
System.out.println ("F4");   
}   
      
public class Test2 extends Test1 {public   
      
void F1 () {   
System.out.println ("Test1 parent method F1 overwritten!");   
      
public static void Main (string[] args) {   
Test2 t=new Test2 ();   
T.f1 ();   
T.f2 (); Invokes the final method T.f3 () inherited from the parent class   
();//Calls methods inherited from the parent class   
//t.f4 ();//call failed, cannot inherit from parent class   
      
}   

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.