static

package com.encapsulation.demo04;

// static
public class Student {
    private static int age; // 静态变量
    private double score; // 非静态变量

    // 匿名代码块  赋初始值
    {
        System.out.println("匿名代码块");
    }

    static { // 全局只加载一次
        System.out.println("静态代码块");
    }

    public Student() {
        System.out.println("构造方法代码块");
    }



    public static void main(String[] args) {
        Student student = new Student();

        System.out.println(Student.age);// 类变量 内存只有一个

    }
}
package com.encapsulation.demo04;
// 静态导入包
import static  java.lang.Math.random;
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
    }
}

总结

  1. static是在实例初始化的时候就创建了,称为类变量,方法内可以直接调用
  2. static方法无法调用普通方法
  3. 方法重写不能定义static
  4. final定义的类无法继承
posted @ 2021-06-29 12:44  橙子yuan  阅读(86)  评论(0)    收藏  举报