[Java-02] In-depth understanding of keywords and code blocks

1 keyword

2 code blocks

1 Keywords in Java

1.1 static keyword

  • statickeyword :
    • Static means, you can modify variables, you can also modify methods, members modified by static, we call static members
  • static features:
    • Static members are shared by all objects of the class
    • Loaded as the class is loaded, prior to object existence
    • It can be called by object or by class name, it is recommended to use class name
  • public class Student {
          
          
        String name;
        int age;
        static String school;
    
        public void show() {
          
          
            System.out.println(name + "---" + age + "---" + school);
        }
    }
    /*
        static关键字的特点 :
            1 静态成员被该类的所有对象的进行共享
            2 静态成员可以通过类名调用 , 也可以通过对象进行调用 , 推荐使用类名
            3 静态成员随着类的加载而加载 , 优先于对象存在
     */
    public class StudentTest {
          
          
        public static void main(String[] args) {
          
          
            Student.school = "XX学院";
    
            Student s = new Student();
            s.name = "张三";
            s.age = 23;
            s.show();
    
            Student s2 = new Student();
            s2.show();
    
        }
    }
    
  • Static memory diagram

    insert image description here

  • Notes on the static keyword
    • Only static members can be called in static methods
    • Any member can be called in a non-static method
    • The this keyword cannot exist in a static method

1.2 final keyword

  • final keyword: Translate Chinese to represent the final meaning. It is a keyword in java and also a modifier. It can modify classes, methods, and variables

  • Features of final keyword modification

    • final modified class: cannot be inherited, no subclass (eunuch class)
    • The method modified by fina: cannot be overridden
    • final modified variables
      • Primitive Data Types: Values ​​cannot be changed
      • Reference data type: the address cannot be changed, and the properties of the object can be changed
      • Notice :
        • Variables modified by final, we call custom constants, naming convention: each letter needs to be capitalized, and multiple words are separated by underscores
        • Final modified member variables need to pay attention to the timing of initialization, 1) direct assignment, 2) assignment before the execution of the construction method is completed
  • package com.itheima.final_demo;
    
    /*
        final的特点
            final修饰的类 , 不能被继承, 也就是没有子类
            final修饰的方法 , 不能被重写
            final修饰的变量
                基本数据类型 : 值不可以发生改变
                引用数据类型 : 地址不可发生改变 , 对象的内容可以发生改变
    
            注意 :
                1 被final修饰的变量 , 我们叫做自定义常量 , 命名规范 : 每个字母需要大写 , 多个单词之间用下划线分割
                2 final修饰成员变量需要注意初始化时机的问题
                  1) 直接赋值
                  2) 在构造方法执行完毕前赋值
     */
    public class FinalDemo1 {
          
          
        public static void main(String[] args) {
          
          
            // final修饰的基本数据类型变量 , 值不能被修改
    //        final int num = 10;
    //        num = 20;
    //        System.out.println(num);
    
            final int[] arr = {
          
          1, 2, 3, 4, 5};
            // final修饰的引用数据类型 , 地址不可改发生改变
            // arr = new int[3];
            // final修饰的引用数据类型 , 对象中的内容可以发生改变
            arr[0] = 100;
    
        }
    }
    
    // final修饰的类 , 不能被继承, 也就是没有子类
    //final class Person {
          
          
    //
    //}
    
    class Person {
          
          
        // final修饰的方法 , 不能被重写
        public final void eat() {
          
          
    
        }
    }
    
    class Student extends Person {
          
          
        // final修饰成员变量需要注意初始化时机的问题
        // 要么直接赋值 , 要么在构造方法执行完毕前赋值
        // final int num = 10;
        final int num;
    
        public Student() {
          
          
            num = 100;
        }
    
        //    @Override
    //    public void eat() {
          
          
    //        super.eat();
    //    }
    }
    
    

1.3 Permission Modifiers in Java

  • public – protected – default – private

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-FdYv689K-1682435036462)(\img\image-20210331215542736.png)]

    package com.bn.permissions_demo1;
    
    /*
        public -- protected -- 默认的 -- private
     */
    public class Fu {
          
          
        public void method1() {
          
          
        }
    
        protected void method2() {
          
          
        }
    
        void method3() {
          
          
        }
    
        private void method4() {
          
          
        }
    
        // 同一个包中同一个类
        public void show(){
          
          
            method1();
            method2();
            method3();
            method4();
        }
    
    }
    ===================================================
    
    package com.bn.permissions_demo1;
    
    public class Test {
          
          
        // 同一个包中 , 无关类
        public void show(){
          
          
            Fu f = new Fu();
            f.method1();
            f.method2();
            f.method3();
            // f.method4();
        }
    }
    ====================================================
    package com.bn.permissions_demo1;
    
    public class Zi extends Fu {
          
          
        // 用一个包中 , 有子类父关系
        public void show(){
          
          
            method1();
            method2();
            method3();
            // method4();
        }
    }
    
    
    package com.bn.permissions_demo2;
    
    import com.bn.permissions_demo1.Fu;
    
    public class Test {
          
          
        // 不同包中 , 无关类
        public void show(){
          
          
            Fu f = new Fu();
            f.method1();
            // f.method2();
            // f.method3();
            // f.method4();
        }
    }
    
    ======================================
        
    package com.bn.permissions_demo2;
    
    import com.bn.permissions_demo1.Fu;
    
    public class Zi extends Fu {
          
          
        // 不同包中 , 有子类父关系
        public void show(){
          
          
            method1();
            method2();
            // method3();
            // method4();
        }
    }
    
    

2 code blocks

2.1 Constructing code blocks

  • Construction method block: represented by a pair of curly braces, defined outside the method in the class
  • Execution timing: Before each construction method is executed, the construction code block will be executed
  • Function: Extract the common content of the construction method
package com.bn.code_block;
/*
    构造代码块
 */
public class Student {
    
    
    final int NUM;

    {
    
    
        NUM = 10;
        System.out.println("构造代码块...");
    }

    public Student() {
    
    
        System.out.println("空参构造...");
    }

    public Student(int a) {
    
    
        System.out.println("有参构造...");
    }
}

class StudentTest {
    
    
    public static void main(String[] args) {
    
    
        Student s = new Student();
        Student s2 = new Student(10);


    }
}

2.2 Static code block

  • Static code block: add the static keyword before a pair of curly braces, defined outside the method in the class

  • Execution timing: loaded as the class is loaded, only loaded once

  • Function: Generally used to initialize static members

    package com.bn.code_block.static_demo;
    /*
      静态代码块 :
          1 定义的位置 : 在一对大括号前加上static , 定义在类中方法外
          2 执行时机 ; 随着类的加载而执行, 只加载一次
          3 可以给类中的静态成员进行初始化数据
     */
    public class Test {
          
          
      public static void main(String[] args) {
          
          
          new Student();
          new Student(10);
      }
    }
    

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


public Student() {
    System.out.println("空参构造");
}

public Student(int a) {
    System.out.println("有参构造");
}

}

### 2.3 局部代码块

- 成员代码块 : 用一对大括号表 , 可以定义在任何的局部位置 , 方法中居多
- 执行时机 : 正常执行(从上往下依次执行)
- 控制变量的局部变量的声明周期

​```java
package com.bn.code_block.local_demo;
/*
  局部代码块 :
      1 位置 : 可以定义任何的局部的位置 , 方法中居多
      2 执行时机 : 正常执行(从上往下依次执行)
      3 作用 : 控制变量的生命周期 , 变量在使用完毕, 及时释放内存
*/
public class Test {
  public static void main(String[] args) {
      int num1 = 10;
      System.out.println(num1);

      // 局部代码块
      {
          int num2 = 20;
          System.out.println(num1);
          System.out.println(num2);
      }

      System.out.println(num1);
//        System.out.println(num2);// 作用域不够 , 报错

  }
}

Guess you like

Origin blog.csdn.net/wanghaoyingand/article/details/130376135