Detailed Java final usage _java

Source: Internet
Author: User

Concept

Final has the meaning of "immutable", which can be decorated with non-abstract, non-abstract member methods, and variables.

    • Classes that are final decorated cannot be inherited without subclasses.
    • The final decorated method cannot overwrite (override) The method of the quilt class.
    • The final-decorated variable represents a constant and can only be assigned a value (when declaring a variable).

Note:

Final cannot be used to decorate the constructor method, because the concept of method overlay applies only to the member methods of the class, not to the constructor of the class, to the constructor of the parent class, and to the construction method of the subclass, so that the final modification of the method has no meaning.

A private-decorated method cannot be overridden by a method of a quilt class, so a method that can be understood as private is the final type by default.

Final class

Define the class as final so that the class cannot be inherited. The specific use of the scene is as follows:

    • is not a class designed specifically for inheritance, and there is a complex invocation relationship between the methods of the class itself. If you arbitrarily create subclasses of these classes, subclasses may incorrectly modify the implementation details of the parent class.
    • For security reasons, the implementation details of the class are not allowed to be extended.
    • When you create the object model, make sure that the class is no longer extended.

For example, the Java.lang.String class in the JDK is defined as the final type.

Public final class string{...}

Final method

In some cases, a parent class does not allow a subclass to overwrite a method for security reasons, at which point the method can be declared as final.

For example, in the Java.lang.Object class in the JDK, the GetClass () method is the final type, and the Equals () method is not final.

All subclasses of Object can override the Equals () method, but cannot override the GetClass () method.

Final variable

The final-decorated variable represents a constant that does not change the value.

For example, two constants are defined in the Java.lang.Integer class in the JDK.

public static final int min_value = 0x80000000;

public static final int max_value = 0x7fffffff;

The final variable has the following characteristics:

    • The final modifier can modify static variables, member variables, and local variables, representing static constants, instance constants, and local constants, respectively.
 public class demo{public
  static final int max_value =;  Static constant public
  static final int min_value = ten;  Static constants
  Private final Date birthday = new Date ();//member Constant
 }

Static constants are generally named in uppercase letters, and words are separated by a "_" symbol.

    • The final decorated member variable must be initialized.
 public class Finaltest {
   final int a = 1;  Member Constants initialize
   static final int b = 2 by default;  Static constants Initialize
   final int c by default;  Member constant
   static final int D;  Static constant public
   
   finaltest () {
     c = 3;  The member constants are not initialized by default, and can be initialized in the constructor
   }
   
   static {
     d = 4;  Static constants are not initialized by default and can be initialized in a static code block.}
 

A final variable can only be assigned a value, and if a variable of a reference type is modified with final, the variable can always refer to an object, but the object's contents may be changed.

 public class Finaltest {
   String str = "";
   public void print () {
     System.out.println (str);
   }  
   public static void Main (string[] args) {
     final finaltest finaltest = new Finaltest ();
     Finaltest.str = "Xixihaha";
     Finaltest.print ();
   }
 

Summarize:

In the actual program, a constant is defined by the final modifier, in order to:

    • Improve the security of your program.
    • Improve the maintainability of your code.
    • Provides code readability.

The above is the entire content of this article, I hope to help you learn.

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.