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 p1 = new Person();
System.out.println("================");
Person p2 = new Person();}}//输出结果
静态代码块
匿名代码块
构造方法
================
匿名代码块
构造方法
Process finished with exit code 0
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
//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();}}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test1 {
public static void main(String[] args){
System.out.println(random());
System.out.println(PI);}}