static关键字
static 静态方法
//static 静态方法
public class Student {
private static int age;//静态的变量 多线程!
private double score;//非静态的变量
public void run() {//非静态方法可以调用静态方法里的所有东西
go();
}
public static void go() {//静态方法可以调用静态方法里的所有东西,
//静态方法 不可以调用非静态变量及方法,如要调用,须先实例化对象进行调用
}
public static void main(String[] args) {//在一个类中,调用方法与静态方法的方式
new Student().run();//对象.方法
Student.go();//类名.方法
go();//方法
System.out.println(age);
System.out.println(new Student().score);//静态方法调用非静态变量,需要先实例化对象
}
}
静态代码块
//静态代码块
public class Person {
//2:赋初值~
{
//代码块(匿名代码块)
//没有名字,程序不会主动调用这些模块
//匿名代码块:创建对象时就自动创建了,而且在构造器之前
System.out.println("匿名