final static modifier, package, anonymous object and code block concepts in java

Knowledge module
1.final modifier
2.static modifier
3. package
4. Permission modifiers
5. Anonymous objects
6. Code blocks



1.final modifier
  a.Modification class
     Classes modified by final cannot be inherited. If you define a class later and do not want it to be inherited by others, you can add final modification.
  b. Modify variables
    final modified variable
      Divided by location: local variables, member variables
      Divided by type: basic type, reference type
         Local variables:
             final modifies local variables and can only be assigned once
         Member variables:
             final modified member variables can only be assigned once
         Reference type:
             Once this variable is finalized, the address value in this reference variable is not allowed to be changed.
  c.Modification method
         Final modified methods can only be used by subclasses and cannot be overridden by subclasses.

/*
 final modified class
     Classes modified by final cannot be inherited. If you define a class later and do not want it to be inherited by others, you can add final modification.
 */
public final class Father {
    public void method() {
        System.out.println("method");
    }
}

2.static modifier
  a. Modify variables
      Local variables:
          static cannot modify local variables
       Member variables:
          static can modify member variables
         use:
            Can be called by objects
            It can also be used by class names and static member variables (can be called directly without creating an object)

  b.Modification method
         Static modified methods can be called through objects or through class names. Static methods (parameters) (the second option is recommended)

  c. Loading timing of static members
         Static members (member variables and member methods) are loaded as the class is loaded, stored in the method area, and destroyed as the class is destroyed (static members and classes live and die together)
         Non-static member variables are loaded as the object is loaded, stored in the heap memory, and destroyed as the object is destroyed (non-static member variables and objects live and die together)
  b. Characteristics of static member variables:
      Non-static member variables are generated with the creation of objects. When you create several objects, there will be several copies of non-static member variables.
      Static member variables are shared by all objects
  e. Static usage details
       static access details
          1. There are no this and super keywords in static methods
             Since static member methods are loaded as the class is loaded, when we pass the class name.static method
             When used, there may not be any object at this time, so our this cannot point to an object.
          2. Static members can directly access static members, but cannot directly access non-static members.
             Static members are loaded as the class is loaded
             Non-static members are loaded as the object is loaded
             Class loading takes precedence over object loading
          3. Non-static members can access both static members and non-static members.


            Static member: Ancient (Li Bai)
            Static member: Ancient (Wang Wei)
            Non-Static Members: People of the 21st Century
  f. Static constant
      We generally define constants for our entire project in one class.
      class Constant{
        /*public: The permissions are large enough and can be accessed by other classes
          static: For ease of use, use it directly through the class name.static member
          final: lock the PI value*/

          public static final constant name=value;
      }

3. package
  a. Why do we need a bag?
     A package is essentially a folder, in order to classify and manage the files in our project
     We generally put related codes that implement the same function under one package
  b. How to define a package?
  1. Naming convention:
    We generally use the reverse writing of the domain name as the name of the package, in order to ensure the uniqueness of the package name
      httpps://www.baidu.com //com.baidu.xx is usually named with a function-related name com.baidu.cloud
      https://www.qq.com //com.qq.xx
      com.package01 //Represents the separation of two folders. The structure of the folder is com folder/package01 folder

      com
         package01
  2. Package definition format:
      package com.package01;//Represents that this Java source file is stored in the com folder/package01 folder
                            //The classes generated after compilation in the future will also
      The package declaration must be placed on the first line of code
  3. Use of packages
      a. If we use classes under the same package, we can use it directly
      b. If you use a class under a different package, you need to use import package name.class name to import it first. You can use this class directly in the code.

  4. Permission modifiers
      permission modifier
                          public default protected private
         In the same category √ √ √ √
         Different classes under the same package √ √ √ X
         Two classes under different packages √ X X X
         Child and parent classes under different packages √ X √ X

         private: Private-modified members can only be used within this class and cannot be used outside the class.
         public: has the greatest permissions and is suitable for cross-package access
         protected: If a member of the parent class is modified with protected, only its subclasses can use it.

  5. Anonymous objects
        Anonymous object (unnamed object)
              Format: new class name (parameter); //Unnamed means no reference variable name
          Usage scenarios: Anonymous objects are mainly used to pass parameters in methods and are only used once.
public class Animal {
    String name;


    public Animal() {

    }
    public Animal(String name) {
        this.name = name;
    }

}
public class Demo01 {
    public static void main(String[] args) {
        //Create an object using empty parameters to construct
        Animal a = new Animal();
        a.name = "小红";
        System.out.println(a.name);
        //Create an object using parameterized construction
        Animal a2= new Animal("小黑");
        System.out.println(a2.name);

        //Anonymous object is constructed using empty parameters
        new Animal().name="Big Bad Wolf";//Assign the name in the first animal object to Big Bad Wolf
        System.out.println(new Animal().name); //Another new animal object is created, but no value is assigned.

        //Anonymous object is constructed using parameters
        new Animal("Little White Rabbit");//"Little White Rabbit" gives the name in the first object
        System.out.println(new Animal("White Rabbit").name);//"Little White Rabbit" gave the name in the second object, and the value of name taken out is also Little White Rabbit

    }
}
public class Demo02 {
    public static void main(String[] args) {
        Animal a1 = new Animal("小白");
        feed(a1);


        feed(new Animal("Little Pig"));//new Animal("Little Pig") assumes its address is 0x3c
    }

    public static void feed(Animal a) {//Animal a=new Animal("小白");
        System.out.println(a.name); //Animal a= new Animal("Little Pig");

    }
}
  6. Code blocks
    a. Local code block
         Local code block: defined in a method
              Limit the scope of variable usage
public class Demo01 {
    public static void main(String[] args) {
        {
            int i=3;
            System.out.println(i);
        }
        //System.out.println(i);
    }
}
 b. Construct code block
         Constructing code blocks: defined in classes
         The construction code block is executed as the object is created, and the construction code block takes precedence over the constructor function.
         Generally, the initialization actions that all objects must be performed are done in the construction code block.
public class Demo02 {
    {
        System.out.println("Construction code block");
    }

    public Demo02() {
        System.out.println("Construction method");
    }

    public static void main(String[] args) {
        new Demo02();
        new Demo02();
    }
}
 ***c. Static code block
         static code block
           Static code blocks are executed as the class is loaded
           We generally initialize static members in static code blocks

public class Demo03 {
    static int i=11;
    static int j;
    static{
        System.out.println("static code block");
        j=13;
    }

    public static void main(String[] args) {
       // new Demo03();
        System.out.println(Demo03.i);
        System.out.println(Demo03.j);
    }
}
 d. Construction code blocks and static code blocks and constructor execution order
         Constructor, static code block, constructed code block execution order
           Static code blocks are executed first, followed by construction code blocks, and finally constructors
           Static code blocks are executed as the class is loaded and only executed once
public class Demo04 {
    {
        System.out.println("Construction code block");
    }

    static {
        System.out.println("static code block");
    }

    public Demo04(){
        System.out.println("constructor");
    }

    public static void main(String[] args) {
        new Demo04();
        System.out.println("--------------");
        new Demo04();

    }
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138953 people are learning the system