Illustrate the use of final keywords in Java _java

Source: Internet
Author: User

1. Final variable

Final variable is a constant that cannot be changed once it is initialized.

Class Test1 {
 final double PI = 3.14;//The name of the constant is best capitalized public

 Test1 () {
  PI = 3.14;
 }

  void Test () {
   System.out.println ("PI is:" + pi);
  }

 public static void Main (string[] args) {
  Test1 t = new Test1 ();
  T.test ();
 }


Output:

PI is:3.14

(1) Blank final variable

Final variable that are uninitialized at the time of the declaration are called Blank final variable, blank final variable must be initialized in the constructor, or a compilation error will be thrown.

Class Test1 {
 final double PI;

 Test1 () {
 pi = 3.14;//Initialization of
 }

  void Test () {
   System.out.println ("PI is:" + pi) in the constructor;
  }

 public static void Main (string[] args) {
 Test1 t = new Test1 ();
 T.test ();
 }


Output:

PI is:3.14

(2) uninitialized static final variable

Static final variable uninitialized in the declaration phase can only be initialized in a static code block

Class Test1 {
 static final double PI;

 static {
 PI = 3.14;
 }

  void Test () {
   System.out.println ("PI is:" + pi);
  }

 public static void Main (string[] args) {
 Test1 t = new Test1 ();
 T.test ();
 }


Output:

PI is:3.14

2. Final method

Final method cannot be overwritten. That is, subclasses can invoke the Fianl method of the parent class, but they cannot overwrite it.

Class Test {
 static final double PI = 3.14;

  final void Test () {
   System.out.println ("PI is:" + pi);
  }

Class Test1 extends test{public

 static void Main (string[] args) {
 Test1 t = new Test1 ();
 T.test ();
 }


Output:

PI is:3.14

3. Final class

Final CALSS cannot be inherited

Final class Test1 {
 static final double PI = 3.14;

  final void Test () {
   System.out.println ("PI is:" + pi);
  }

 public static void Main (string[] args) {
 Test1 t = new Test1 ();
 T.test ();
 }


Output:

PI is:3.14

Why is the

PS:java.lang.String class designed to be final?
First, you need to know the final keyword first.
Final appears to have two points for reasons that don't want to change: design or efficiency. The final-decorated class is not inheritable, so the final-decorated class cannot be tampered with.
with this in mind, let's look at the problem:
(1) from the design,
A, make sure that they don't change semantics in subclasses. The string class is the final class, which means that no one is allowed to define a subclass of string. In other words, if you have a string reference, it must be a string object, not an object of another class.
B, string once created can not be modified, because the Java designer will String to be shared, the following is the source of the comments:
(2) from the efficiency:
A, designed to FINAL,JVM not to the relevant methods in the virtual function table query, The execution efficiency is improved by directly locating the related methods of the String class.
B, the Java designer believes that sharing is more efficient.
In summary, it is necessary to ensure that the object referenced by the Java.lang.String reference must be an object of java.lang.String, not a descendant of it, in order to ensure its efficiency and security.

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.