Summary of static and final usage in Java

Source: Internet
Author: User

Final
    • Final Variable (Before the keyword final is added to the defined variable, the variable is not changed once it is initialized.)

      • The specific meaning of an immutable term is:
        • Basic type whose value cannot be changed
        • Object variable whose reference cannot be changed
      • Its initialization can be done in two places (both can be selected):
        • The definition of a variable (that is, assigning a value directly to the final variable definition)
        • In the constructor
      • When a function parameter is a final type, it can be read using the parameter, but the value of the parameter cannot be changed.
      • The inner class defined inside the method must also be declared final to use if the parameter of the method is required.
    • Final Method (If a class does not allow its subclasses to overwrite a method, declare this method as the final method)
      There are two reasons for using the final method:

      1. The locking method prevents any inheriting class from modifying its meaning and implementation.
      2. efficient . The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.
    • Final Class (final class cannot be inherited )

      • Therefore, the member methods of the final class have no chance to be overwritten, and the default is final to decorate
      • When designing a class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change then it is designed as the final class
Static
  1. Overview
    member variables and member methods that are modified by static are independent of any object of that class.
    It belongs to the class itself and does not depend on class-specific instances, shared by all instances of the class

    • that is, static means that you do not need to instantiate to use
    • as long as this class is loaded, the JVM can be found in the method area of the runtime data area based on the class name.
    • A
    • static object can be accessed before any of its objects are created, without referencing any objects.
    • a private adornment before a static variable means that cannot refer directly to a class name in another class
    • The
    • static variable can be used in a static code block of a class, or in a member method of a class (including static and non-static)
  2. The
  3. static variable
    is divided into two categories according to whether the class member variable is static:

    • static variable preceded by a static decoration (also called strong> class variable )
    • instance Variable There is no static modifier on it

      The difference between the two is as follows:

    • for static variable
      1. Save memory only one copy in memory, complete memory allocation to static variables during JVM load class
      2. Easy Direct access with the class name (which can also be accessed through objects, but not recommended)
    • for instance variables
      1. Flexible There can be multiple copies in memory that do not affect each other
      2. allocates memory once for each instance that is created.
        • static method
        • static method to call directly through the class name , Any instance can also invoke the /strong> the
        • static method can only access statically member variables and member methods of the owning class .
        • The This and Super keywords cannot be used in the
        • static method
        • The
        • static method cannot be modified by the abstract because it is independent of any instance and must have a method body inside the class
  4. Static code block
    • These static blocks of code are executed when the JVM loads the class, and each code block is executed only once
    • Also called a static block of code, which is the static statement block that is independent of the class member in the class.
    • can have multiple, position can be placed casually, it is not in any method body
    • If there are multiple static code blocks, the JVM executes them sequentially in the order in which they appear in the class.
Final static appears simultaneously

Static final is used to modify member variables and member methods, so that you can easily understand global constants
for variable representations, once a value is given, it cannot be modified and can be accessed through the class name.
The method representation is not overwritten and can be accessed directly through the class name.

Summary of static and final usage 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.