• Java — static修饰符


    静态属性
    1.概要
    • 定义语法:权限修饰符 static 数据类型 属性名称;

      public static int num;
      
      • 1
    • 使用方法:类名.属性名

    • 意义:静态属性的值所有对象共用,实现了数据的共享

    2.特点
    • 声明为static的属性也被称为类属性
    • 当创建一个对象时,并不产生static属性的拷贝,即所有的实例共享同一个static属性
    • 在类装载时,只分配一块存储空间
    • 静态属性值一旦改变,所有类的对象均共享改变
    3.示例4:统计创建对象的个数
    public class Student {
        public static int count; //静态属性的值所有对象共享
        private String name;
    
        public Student(){
            count ++;
        }
    
        public int getCount() {
            //成员方法中可以直接访问静态属性(不加静态修饰)
            return count;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    public class Text {
        public static void main(String[] args){
    
            Student.initCount();//静态方法:可通过类名.静态方法名访问
            System.out.println(Student.count);//0
            //在创建任何学生对象之前,可以通过类名访问(公有类)
    
            Student stu1 = new Student();
            System.out.println(stu1.count);//1
            Student stu2 = new Student();
            System.out.println(stu2.count);//2
            //public修饰的静态属性,建议用此种访问方法
            System.out.println(Student.count);//2
    
            System.out.println(stu1.getCount());//2
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    静态方法
    1.概要
    • 定义语法:权限修饰符 static 返回值类型 方法名(形式参数列表){ …方法体 }

      public static void initCount(){
          count = 20;
      }
      
      • 1
      • 2
      • 3
    • 功能:访问静态属性(私有静态成员

    • 特点:不调用不执行

    2.访问方式

    与静态属性一样,有两种访问方式

    • 类名.公有静态方法名([参数])
    • 对象名.公有静态方法名([参数])
    3.类内部使用规则
    • 在静态方法中,仅能直接调用其他的 static 类型的方法。

    • 在静态方法中,只能直接访问 static 类型的属性。

    • 在静态方法中,不能以任何方式使用 this 或 super。

      public static void initCount(){
          //完成对静态属性的操作
          count = 20;
          //不能直接操作成员属性,不能调用成员方法
          //name  = "吴小祖";//编译错误
          //getName();
      
          //this.name = "吴小祖";//在静态方法中不可以使用thisuper
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    静态代码块
    1.概要
    • 静态代码块就是用static修饰的代码块
    • 功能:初始化static变量
    • 特点:随着类的加载而执行,而且只执行一次
    • 涉及到继承的时候,会先初始化父类的static代码块,然后是子类的静态代码块
    2.子类实例化对象执行顺序
    • 先静态后构造
    • 先父类再子类
    3.实例
    //初始化`static`变量
    //随着类的加载而执行,而且只执行一次
    static {
        System.out.println("静态代码块");
        count = 10;
        //		name = "abc";//编译出错
    }
    
    //非静态代码块:每创建一个对象,就会自动执行一次
    {
        System.out.println("非静态代码块");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    4.类加载到内存的时机
    • 通过类名访问静态属性或者调用静态方法时

    • 创建某个类的对象时

    • new一次只执行一次,不管创建了几个对象

  • 相关阅读:
    ssh远程使用jupyter notebook
    使用外部时钟,通过TIM21_CH1,对STM32L0XX内部的RC时钟(HSI/MSI等)进行校准
    微积分入门书籍(一)
    详解 BAT 面试必问之 ThreadLocal(源码 + 内存)
    Docker 安装最新版本 Jenkins
    18. 四数之和
    学习STM32(2)--STM32单片机GPIO应用
    无法启动此程序,因为计算机中丢失MSVCR71.dll的详细解决修复方法
    自定义注解、数字字典注解
    MathType需要安装一个较新版本的MT Extra(TrueType)字体解决办法
  • 原文地址:https://blog.csdn.net/m0_60610120/article/details/127650941