static,程式碼塊,final
阿新 • • 發佈:2021-08-08
-
靜態修飾的屬性和方法隨著類載入一同載入(全域性變數?)
-
靜態方法不能呼叫非靜態方法,非靜態方法可以呼叫靜態方法
-
靜態屬於類,非靜態屬於物件
程式碼塊
public class Student {
//2 賦初始值
{
System.out.println("匿名程式碼塊");
}
//1 只執行一次
static {
System.out.println("靜態程式碼塊");
}
//3
public Student(){
System.out.println("構造方法");
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println("===============");
Student s2 = new Student();
}
}
/*
靜態程式碼塊
匿名程式碼塊
構造方法
===============
匿名程式碼塊
構造方法
*/
靜態匯入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}
final修飾符
-
final是常量修飾符,不能被再次更改
-
被final修飾的類不能被繼承