对于属性和方法
public class Student {private static int age; // 静态的变量 共享这一个数据 多线程用的多private double score; // 非静态的变量public void run(){go();}public static void go(){}public static void main(String[] args) {Student s1 = new Student();System.out.println(Student.age);
// System.out.println(Student.score); // Non-static field 'score' cannot be referenced from a static contextSystem.out.println(s1.age);System.out.println(s1.score);new Student().run();Student.go();go();}
}
静态代码块
public class Person {// 2 可以用来 赋初始值{// 代码块 (匿名代码块)System.out.println("匿名代码块");}// 1static {// 静态代码块 类加载时执行,永久只执行一次System.out.println("静态代码块");}// 3public Person() {System.out.println("构造方法");}public static void main(String[] args) {Person person = new Person();System.out.println("=================");Person person1 = new Person();}
}
final关键字修饰的类不能被继承
https://www.bilibili.com/video/BV12J41137hu?p=73