static关键字
public class Student {
private static int age;
private double score;
public void run(){
go();//非静态方法可以调用静态方法
}
public static void go(){
//run(); //static方法是和类一起加载的,加载时,run()还没加载,所以不能直接调用
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
//System.out.println(Student.score);
System.out.println(s1.age);
System.out.println(s1.score);
(new Student()).run();//static方法是和类一起加载的,加载时,run()还没加载,所以不能直接调用,只能通过new来调用
go();
}
}
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();
}
}
/*
静态代码块
匿名代码块
构造方法
================
匿名代码块
构造方法
*/
增加知识点
1 //静态导入包
2 import static java.lang.Math.random;
3 import static java.lang.Math.PI;
4
5 public class Test {
6 public static void main(String[] args) {
7 System.out.println(random());
8 System.out.println(PI);
9 }
10 }

浙公网安备 33010602011771号