static
//静态变量对于类,所有对象(实例)所共享,当直接使用类去调用得到说明这个变量是静态
public class Student {
private static int age; //静态变量
private double score; //非静态变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
new Student().run(); //通过对象.方法来使用
Student.go(); //静态
go(); //静态 static与类一起加载
}
}
/*
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.age);
System.out.println(s1.score);
*/
public class Person {
{
System.out.println("匿名代码块"); //第二个执行
}
static {
System.out.println("静态代码块"); //第一个执行 static只执行一次
}
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;
import static java.lang.Math.PI;
//如果不加static是不可以在Math后加具体方法的
//import java.lang.Math.random; 会报错
public class Application {
public static void main(String[] args) {
System.out.println(Math.random()); //返回一个随机数
System.out.println(PI);
}
}