static:
1.对类中成员以及方法的定义
2.静态代码块与匿名代码块
3.静态进口包
//static
public class Student{
private static int age; //静态变量
private double score; //非静态变量
public void run(){
System.out.println("run");
}
public static void go(){
System.out.println("go");
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);//使用类名访问静态变量(静态变量只有一个)
Student.go();//使用类名访问静态成员函数(静态成员函数只有一个)
go();//静态方法的直接调用机理:在加载类时直接生成静态的方法
//run();//动态方法不能够直接调用,在加载类时不会跟随生成
}
}
public class Person{
//顺序2
{
//代码块(匿名代码块):可用来赋初始值
System.out.println("匿名代码块");
}
//顺序1
static {
//静态代码块
System.out.println("静态代码块");
}
//顺序3
public Person() {
System.out.println("构造方法");
}
public static void main(String []args){
Person person1 = new Person();
System.out.println("==============");
Person person2 = new Person();
}
}
测试结果://通过测试结果可以看到:静态代码块只调用一次,且最先执行 //匿名代码块可调用多次,且顺序在静态代码块后面,构造 器反而是三者中最后执行的
import static java.lang.Math.random;//静态导入包
public class Test {
public static void main(String[] args) {
System.out.println(random());
//Math.random()简化写法
}
}



