Three modifiers in Java [abstract, static, final] (7)

abstract ? ( abstract ) of the first modifier

First, [abstract] can modify the class

( 1 ) The class modified by abstract is called abstract class
( 2 ) Syntax:
           abstract  class class name {}
( 3 ) Features: abstract classes cannot create objects alone, but can declare references
          abstract class class name reference name;
( 4 ) An abstract class can define member variables and member methods
( 5 ) The abstract class has a constructor, when it is used to create a subclass object, the jvm creates a parent class object by default;
    The abstract constructor is applied when the JVM creates the parent class object.

Second, abstract can modify the method

( 1 ) The method modified by asbtract is called abstract method
( 2 ) Syntax:
    access modifier abstract   return value type method name (parameter list);
    Note: abstract and access modifiers have no ordering requirements
( 3 ) Features: abstract methods have only the declaration part, and no implementation part of the method (not even {}, ending with ;)
( 4 ) Note: abstract methods can only be defined in abstract classes; but abstract classes can define both abstract methods and non-abstract methods

[]Subclass of [abstract] class :

( 1 ) Syntax:
     class subclass class name extends abstract class class name {}
( 2 ) Requirement: If the subclass does not want to be an abstract class, it must override all abstract methods in the parent class of the abstract class (purpose: to supplement the implementation of the abstract method);
    If the subclass does not cover all abstract methods in the parent class, it must be defined as an abstract class, and objects cannot be created at the same time
( 3 ) Application: the abstract class embodies the application of polymorphism
    Abstract class class name reference name = new sub class class name ();   // The reference of the super type stores the object of the sub type

The second modifier of static ? ( static ) — ( this is very important~ )

  1. [Static] can modify properties

( 1 ) Properties modified by static are called static properties, static variables, and class variables
    Note: Member variables are divided into: instance variables and static variables (or static properties, class variables)
( 2 ) Position: defined within the class, outside the method, modified by static
( 3 ) Syntax: access modifier static data type variable name;
         access modifier static data type variable name = value;
    Note: There is no order requirement between access modifiers and static , but they must be in front of the data type
( 4 ) Features: Static properties exist based on classes, regardless of how many objects are created, and are shared by all objects
( 5 ) use:
        a. By object name.Static property name
        b. Directly through the class name. Static property name --> suggestion

    Note: Instance variables must be accessed via objectname.instance variablename

Two, static can modify the method

( 1 ) A method modified by static is called a static method
( 2 ) Syntax:
    Access modifier static return value type method name (formal parameter list) {
             // method implementation, method body
    }Note: There is no ordering requirement between  
    access modifiers and static
( 3 ) use:
    a. Directly pass the class name. Static method name (argument); -- "Suggestion
    b. By object name. Static method (argument); --> Not recommended
( 4 ) Static method syntax details:
    a. Only static members of this class (static properties and static methods) can be accessed in static methods
    b. Non-static members of this class cannot be directly accessed in static methods (instance variables + non-static methods)c. The this / super keyword 
    cannot be used in static methods
    d. Static methods can be inherited by subclasses
    e. Static methods can only be overridden by static methods, and static methods do not reflect the application of polymorphism
( 5 ) Static method application scenario: Usually, the method in the tool class is set as a static method, in order to facilitate the user to call

Three, static can modify the initialization code block

( 1 ) The initialization code block modified by static is called static code block
( 2 ) The location of the static code block: defined within the class, the method is that the {}
         class class name modified by static { static {
                 // static code block

            }
        }
( 3 ) Function: When the class is loaded, the initialization of the static properties is completed according to the sequence defined with the static properties    
( 4 ) Class loading:
    a. Concept: When jvm uses a class for the first time, it finds the .class file corresponding to the class through classPath;
            And read the information of the class from the .class file;
            (package name, class name, parent class, attribute, constructor, member method, etc.);
            Save the read information to the jvm memory, a class is only loaded once.

    b. Timing of class loading: (what is the first time jvm uses a class)
        I. The first to access the static members of the class (static properties and static methods)
        II. Create the object of this class for the first time: complete the class loading first; then complete the creation of the object
        III. Subclass class loading, first cause its parent class to load: add it to the parent class first, and then load the subclass
            ① The first call to a subclass static property or static method
             ② Create a subclass object for the first time: first load the class, and then complete the creation of the object
                Loading: First complete the class loading of the parent class, and then complete the class loading of the child class

                Create an object: first complete the creation of the parent class object, and then create the subclass object

The third modifier final ? ( final )

  1. Final can modify variables ( local variables, member variables -> instance variables and static variables )

( 1 ) Features: variables modified by final , constants in the scope, only allow one assignment, and can be used multiple times
    Note: Once a final modified variable is assigned, it cannot be modified

( 2 ) Syntax:
    access modifier final data type variable name = value;

( 3 ) Final -modified instances no longer have default values, and developers have the following opportunities to assign values ​​to them:
        a. It is initialized and assigned when it is defined
                final  int a = 3 ;
        b. Use the constructor to complete the assignment
                class  A {
                     final  int a ;
                    public  A ( int a) {
                         this .a=a;
                    }
                }
( 4 ) Final modified static variables no longer have default values, and developers have the following opportunities to assign values ​​to them:
        a. Initialize it at the time of definition, assign a value
        b. Initialize it with a static code block
                class  A {
                     final  static  int n;
                    static {
                        n = 5;
                    }
                }
( 5 ) The final modified reference means that the object stored in the reference cannot be changed

Second, final can modify the method

Can be inherited by subclasses, but subclasses are not allowed to override.

Third, final can modify properties

A class modified by final cannot be inherited, that is, it has no subclasses.

Leave a Comment

Your email address will not be published. Required fields are marked *