Static
目前如果想要調用某個對象上面的方法,必須要產生對象的執行個體,如果在兩個地方調用就需要產生兩個執行個體。有些時候被調用的方法與執行個體的多少沒有任何關係,該方法可能只是協助方法。在這種情況下,不需要多個執行個體,為瞭解決必須產生執行個體才能調用方法的問題,java 引入了static,先看一個執行個體:
public static void main(String[] args) {
System.out.println(Math.random());
}
在jdk中Math的random方法的作用是提供一個產生隨機數的功能,它只是一個協助方法,與Math中的執行個體變數沒有任何關係,所以不需要調用一次產生一個執行個體,只通過類直接調用就可以了,在此JDK中就提供了random這static(靜態)方法。
Static 不僅可以修飾方法還可以修飾屬性,由於Static 修飾的方法或者屬性與執行個體的多少沒有任何的關係,可以理解為static 的方法和屬性是可以在多個執行個體之間共用的。
看以下執行個體:
public class StaticDemo {
static int counter;
public StaticDemo(){
counter++;
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
StaticDemo sd1 = new StaticDemo();
System.out.println("counter:"+counter);
}
}
運行結果:counter:2
從以上執行個體可以看出,sd0 所指向的對象和 sd1指向的對象共用了static變數。實際上我們經常提到的入口函數也是個static靜態方法,靜態方法與執行個體沒有任何的關係,它可以直接調用靜態變數。 也可以通過執行個體的引用調用靜態屬性或者方法,效果是相同,只是不需要而已。
以上述執行個體為例:
System.out.println("counter:"+sd0.counter);, System.out.println("counter:"+sd1.counter)和 System.out.println("counter:"+counter);三者完全等價。
再強調一遍,在靜態方法裡可以直接調用static(靜態)變數,如果不是靜態變數,而是執行個體變數,是不能夠在靜態方法中直接調用的,必須要先產生執行個體,好好理解以下這句話:執行個體變數是與執行個體相關的,只有產生了執行個體,執行個體變數才有意義。而我們想要調用執行個體變數,這時候必須要先有執行個體,所以如果想要調用執行個體變數必須 new 出個執行個體。
以上執行個體,如果把關鍵字static去掉,則counter就變成了執行個體變數,如果在static 方法中調用必須先有一個執行個體。
靜態代碼塊
可以把某段代碼直接通過static修飾,看以下執行個體:
public class StaticDemo {
int counter;
static {
System.out.println("in the static segment...");
}
public StaticDemo(){
System.out.println("in the constuctor segment...");
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
}
}
運行結果:
in the static segment...
in the constuctor segment...
大家注意到,通過static修飾的代碼塊在建構函式之前就執行了,實際上靜態代碼塊是在程式載入(load)的時候執行的,而建構函式是在啟動並執行時候執行的。
靜態代碼塊只載入一次,看以下執行個體:
public class StaticDemo {
static {
System.out.println("in the static segment...");
}
public StaticDemo(){
System.out.println("in the constuctor segment...");
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
StaticDemo sd1 = new StaticDemo();
StaticDemo sd2 = new StaticDemo();
}
}
運行結果:
in the static segment...
in the constuctor segment...
in the constuctor segment...
in the constuctor segment...
從運行結果可以看出static 代碼只執行了一次,建構函式執行了三次,也就是靜態代碼塊會
在載入的時候執行,而且只執行一次。
匿名代碼塊。
沒有方法名的代碼塊可以通過static修飾,那麼如果我們不用static修飾,不在代碼塊上加任何的修飾符,列印順序會是什麼呢?
看以下執行個體:
public class StaticDemo {
int counter;
{
System.out.println("in the normal segment...");
}
static{
System.out.println("in the static segment...");
}
public StaticDemo(){
System.out.println("in the constuctor segment...");
}
public static void main(String[] args) {
StaticDemo sd0 = new StaticDemo();
}
}
我們注意到在static代碼塊之前有有沒有沒有任何修飾沒有函數名的代碼塊,我們稱之為匿名函數。匿名函數會在載入後但是在任何函數之前得到執行,實際上匿名函數是與執行個體有關係的,也就是需要產生執行個體之後執行,但是運行結果確實在建構函式之前得到執行。實際上在建構函式執行之前,執行個體已經存在,建構函式的作用只是初始化或者賦值的。在學習建構函式的時候,經常會在建構函式中看到this關鍵子,比如以下代碼:
public Dog(String dogName,int age ){
this.dogName = dogName;
this.age = age;
}
以上的this 代表當前對象,也就是說在執行建構函式中代碼的時候對象已經存在,此處的建構函式完成的是賦值操作。所以可以理解為new 的動作是分為幾步完成的
1. 產生對象,2。初始化。
public class User {
private int age ;
{
System.out.println("in the segment"+age);
}
public User(){
this.age = 1;
System.out.println("in the constuctor method.."+age);
}
static {
System.out.println("in the static segment..");
}
}
入口函數:
public static void main(String[] args) {
User user = new User();
}
運行結果:
in the static segment..
in the segment0
in the constuctor method..1