static关键字详解
- 非静态方法可以调用静态的方法里的方法
- 静态方法不能调用普通的方法
- main方法是static的
- 静态代码块只执行一次
package com.oop.Demo07;
public class Student {
private static int age;
private double score;
public void run(){
go();
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.score);
System.out.println(s1.age);
Student.go();
}
}
package com.oop.Demo07;
public class Person {
{
System.out.println("匿名代码块");
}
static{
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("======================");
Person person2 = new Person();
}
}
package com.oop.Demo07;
import static java.lang.Math.PI;
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}