Java學習筆記八---類的靜態變數與靜態方法的訪問與調用方式,java學習筆記
靜態變數又稱類變數,靜態方法又稱類方法,它們統稱為靜態成員或類成員。靜態成員由static修飾,是屬於整個類的,所有的對象共用這些靜態成員。不需要建立任何對象,靜態成員在類裝載時就已初始化了,整個運行期間其記憶體位置不變,直到類卸載。鑒於靜態成員的這些特性,訪問靜態變數以及定義或調用靜態方法時與非靜態成員也有不同之處。下面分別做出說明。
1.靜態變數
- 類的非靜態方法、靜態方法都可以直接存取靜態變數
- 其他類要訪問某個類的靜態變數,既可以通過執行個體名訪問,也可以直接用類名來訪問,推薦用類名訪問的方式,這樣能更直觀的說明訪問的變數是靜態變數
2.靜態方法
- 不能直接存取非靜態變數,也不能直接調用非靜態方法,需要執行個體化一個對象,再通過該對象來訪問要訪問的非靜態變數或要調用的非靜態方法。也就是說,靜態方法不能直接使用非靜態成員。個人理解是,非靜態成員是依託對象而存在的,當沒有執行個體一個對象時,非靜態成員是沒有分配記憶體空間的,靜態方法要使用非靜態成員不知道要到哪裡去找,當然就不能直接使用非靜態成員了。
- 其他類要調用某個類的靜態方法,既可以通過執行個體名調用,也可以直接用類名來調用,推薦用類名調用的方式,這樣能更直觀的說明調用的方法是靜態方法
3.下面以簡單的代碼驗證上面的結論
定義了一個Person類,類的代碼見最後面。
(1).類的靜態方法可以直接存取靜態變數
- 靜態方法staticMethod訪問了靜態變數citizenship
- 定義並使用了局部變數testY
staticMethod()方法如下:
public static void staticMethod() {int testY = 20;System.out.println("She has applied for " + citizenship + " citizenship"); //static variable accessSystem.out.println("She's now " + testY + " years old"); //local variable access
}
main()方法如下:
public static void main(String[] args) {Person xiaoxi = new Person("xiaoxi",29,"female","piano");xiaoxi.informationPrint();staticMethod();}
輸出結果如下:
My name is xiaoxiI am 29 years oldI am a girlMy hobby is pianoI am a chineseShe has applied for Chinese citizenshipShe's now 20 years old
結果分析:
- 靜態方法可以直接存取靜態變數
- 靜態方法可以自訂局部變數
(2),靜態方法不可以直接存取非靜態變數
[1].staticMethod直接存取靜態變數citizenship,出現錯誤
- 靜態方法staticMethod訪問了靜態變數citizenship
- 定義並使用了局部變數testY
- 訪問了非靜態變數hobby
staticMethod()方法如下:
public static void staticMethod() {int testY = 20;System.out.println("She has applied for " + citizenship + " citizenship"); //static variable accessSystem.out.println("She's now " + testY + " years old"); //local variable accessSystem.out.println("She doesn't like " + hobby); //nonstatic variable access}
main方法同上。
輸出結果如下:
My name is xiaoxiI am 29 years oldI am a girlMy hobby is pianoI am a chineseException in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field hobbyat human.Person.staticMethod(Person.java:99)at human.Person.main(Person.java:107)
結果分析:
- 靜態方法不能直接存取非靜態變數,否則會出現“Cannot make a static reference to the non-static field hobby” 的錯誤。
[2].staticMethod()建立一個對象testP,由testP訪問非靜態變數hobby,成功執行
- 靜態方法staticMethod使用了靜態變數citizenship
- 定義並使用了局部變數testY
- 建立一個Person執行個體testP,並使用了testP的變數hobby
staticMethod()方法如下:
public static void staticMethod() {int testY = 20;Person testP = new Person();System.out.println("She has applied for " + citizenship + " citizenship"); //static variable accessSystem.out.println("She's now " + testY + " years old"); //local variable accessSystem.out.println("She doesn't like " + testP.hobby); // nonstatic variable access via object instance testP}
main方法同上。
輸出結果如下:
My name is xiaoxiI am 29 years oldI am a girlMy hobby is pianoI am a chineseShe has applied for Chinese citizenshipShe's now 20 years oldShe doesn't like null
結果分析:
- 靜態方法要訪問非靜態變數,可以先執行個體化一個對象,再通過對象訪問。
(3),靜態方法不可以直接調用非靜態方法
[1].staticMethod()直接存取非靜態方法informationPrint(),出現錯誤
- 靜態方法staticMethod使用了靜態變數citizenship
- 定義並使用了局部變數testY
- 建立一個Person執行個體testP,並使用了testP的hoppy變數
- 直接調用非靜態方法informationPrint()
staticMethod()方法如下:
public static void staticMethod() {int testY = 20;Person testP = new Person();System.out.println("She has applied for " + citizenship + " citizenship"); //static variable accessSystem.out.println("She's now " + testY + " years old"); //local variable access//System.out.println("She doesn't like " + testP.hobby); //nonstatic variable accessSystem.out.println("She doesn't like " + testP.hobby); // nonstatic variable access via object instance testPSystem.out.println("Her personal information is as follows:");informationPrint();}
main()方法同上。
輸出結果如下:
My name is xiaoxiI am 29 years oldI am a girlMy hobby is pianoI am a chineseException in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static method informationPrint() from the type Personat human.Person.staticMethod(Person.java:103)at human.Person.main(Person.java:111)
結果分析:
- 靜態方法不能直接調用非靜態方法,否則會出現與直接存取非靜態變數類似的錯誤,Cannot make a static reference to the non-static method informationPrint() from the type Person。
[2].通過已有對象testP來調用informationPrint()方法,成功執行
- 靜態方法staticMethod使用了靜態變數citizenship
- 定義並使用了局部變數testY
- 建立一個Person執行個體testP,並使用了testP的hoppy變數
- 通過testP調用非靜態方法informationPrint()
staticMethod()方法如下:
public static void staticMethod() {int testY = 20;Person testP = new Person();System.out.println("She has applied for " + citizenship + " citizenship"); //static variable accessSystem.out.println("She's now " + testY + " years old"); //local variable accessSystem.out.println("She doesn't like " + testP.hobby); // nonstatic variable access via object instance testPSystem.out.println("Her personal information is as follows:");//informationPrint();testP.informationPrint();}
main()方法同上。
輸出結果如下:
My name is xiaoxiI am 29 years oldI am a girlMy hobby is pianoI am a chineseShe has applied for Chinese citizenshipShe's now 20 years oldShe doesn't like nullHer personal information is as follows:My name is nullI am 0 years oldSomething is wrong!My hobby is nullI am a chinese
結果分析:
- 靜態方法要調用非靜態方法,可以先執行個體化一個對象,再通過該對象來調用。
附Person類:
package human;public class Person {String name;int age;String gender;private String hobby;protected String residence;static String citizenship = "Chinese";public Person() {}public Person(String n, String g) {this.name = n;this.gender = g;}public Person(String n, int a, String g, String h) {this.name = n;this.age = a;this.gender = g;this.hobby = h;//test:靜態變數初始化的時機是否在構造方法之前System.out.println("constructor:");System.out.println("change value of the static variable citizenship " + "\"" + citizenship + "\"");citizenship = "US";System.out.println(" to " + "\"" + citizenship + "\"");}public Person(String n, int a, String g, String h, String r) {this.name = n;this.age = a;this.gender = g;this.hobby = h;this.residence = r;}public void setName(String n) {this.name = n;}public void setAge(int a) {this.age = a;}public void setGender(String g) {this.gender = g;}public void setHobby(String h) {this.hobby = h;}public void setResidence(String r) {this.residence = r;}public String getName() {return this.name;}public int getAge() {return this.age;}public String getGender() {return this.gender;}public String getHobby() {return this.hobby;}public String getResidence() {return this.residence;}public void informationPrint() {System.out.println("My name is " + getName());System.out.println("I am " + getAge() + " years old");if(getGender() == "female")System.out.println("I am a girl");elseif(getGender() == "male")System.out.println("I am a boy");elseSystem.out.println("Something is wrong!");System.out.println("My hobby is " + hobby);if(citizenship == "Chinese")System.out.println("I am a chinese");//test:靜態變數是否在構造方法之前初始化elseif(citizenship == "US")System.out.println("I am an American");elseSystem.out.println("Oh,something is wrong");}public static void staticMethod() {int testY = 20;Person testP = new Person();System.out.println("She has applied for " + citizenship + " citizenship"); //static variable accessSystem.out.println("She's now " + testY + " years old"); //local variable accessSystem.out.println("She doesn't like " + testP.hobby); // nonstatic variable access via object instance testPSystem.out.println("Her personal information is as follows:");//informationPrint();testP.informationPrint();}public static void main(String[] args) {Person xiaoxi = new Person("xiaoxi",29,"female","piano");xiaoxi.informationPrint();staticMethod();}}