徹底填平Static坑——細(xì)節(jié)決定成敗
學(xué)習(xí)的過程就是填坑的過程,可不要偷懶想著跳過去,你現(xiàn)在跳過去,就相當(dāng)于給自己挖了一個(gè)坑,你遲早會(huì)掉進(jìn)去的,為了避免不掉坑,所以,努力填坑吧!
如果沒有static會(huì)怎樣?
需求:
1、定義Student類
- 姓名、國籍,說話行為
- 多個(gè)構(gòu)造,重載形式體現(xiàn)
2、學(xué)生的國籍都是確定的
- 國籍可以進(jìn)行顯示初始化
- public class Student {
- String name;//姓名
- String country;//國籍
- public Student(String name, String country) {
- this.name = name;
- this.country = country;
- }
- public void speak(){
- System.out.println("姓名:"+this.name+" "+"國籍:"+this.country);
- }
- }
- class Test{
- public static void main(String[] args) {
- Student student = new Student("何道昌","中國");
- Student student1 = new Student("孫雙雙","中國");
- student.speak();
- student1.speak();
- }
- }
-
運(yùn)行結(jié)果:
姓名:何晶晶 國籍:中國
姓名:孫雙雙 國籍:中國
目前存在的問題:
現(xiàn)在我們已知學(xué)生都是中國人,現(xiàn)在我們每創(chuàng)建一個(gè)學(xué)生對(duì)象,就要給所有學(xué)生的國籍屬性賦相同的值,這樣造成堆內(nèi)存空間資源浪費(fèi)
目前方案:
把“中國”這個(gè)數(shù)據(jù)移動(dòng)到數(shù)據(jù)共享區(qū)中,共享這個(gè)數(shù)據(jù)給所有的Student對(duì)象使用即可
疑問:如何才能把這個(gè)數(shù)據(jù)移動(dòng)到數(shù)據(jù)共享區(qū)中共享呢?
解決方案:
只需要用static修飾該數(shù)據(jù)即可
靜態(tài)的成員變量只會(huì)在數(shù)據(jù)共享區(qū)中維護(hù)一份,而非靜態(tài)成員變量的數(shù)據(jù)會(huì)在每個(gè)對(duì)象中都維護(hù)一份
- public class Student {
- String name;//姓名
- //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù)
- static String country = "中國";//國籍
- //構(gòu)造函數(shù)
- public Student(String name) {
- this.name = name;
- }
- //說話行為
- public void speak(){
- System.out.println("姓名:"+this.name+" "+"國籍:"+country);
- }
- }
- class Test{
- public static void main(String[] args) {
- Student student = new Student("何道昌");
- Student student1 = new Student("孫雙雙");
- student.speak();
- student1.speak();
- }
- }
運(yùn)行結(jié)果:
姓名:何晶晶 國籍:中國
姓名:孫雙雙 國籍:中國
下面我來詳細(xì)解說static
static(靜態(tài)修飾符)
1.static修飾靜態(tài)變量
如果有數(shù)據(jù)需要被共享給所有對(duì)象使用時(shí),那么就可以使用static修飾
靜態(tài)成員變量的訪問方式:
方式一:可以使用對(duì)象進(jìn)行訪問
格式:對(duì)象.變量名
方式二:可以使用類名進(jìn)行訪問
格式:類名.變量名
注意:
1). 非靜態(tài)的成員變量只能使用對(duì)象進(jìn)行訪問,不能使用類命進(jìn)行訪問
- public class Student {
- String name;//姓名 非靜態(tài)成員變量
- //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù)
- static String country = "中國";//國籍 靜態(tài)成員變量
- //構(gòu)造函數(shù)
- public Student(String name) {
- this.name = name;
- }
- //說話行為
- public void speak(){
- System.out.println("姓名:"+this.name+" "+"國籍:"+country);
- }
- }
- class Test{
- public static void main(String[] args) {
- Student student = new Student("何道昌");
- System.out.println(student.name);//用對(duì)象訪問非靜態(tài)變量<br> Systen.out.println(student.country);//用對(duì)象訪問靜態(tài)變量
- System.out.println(Student.country);//用類命訪問靜態(tài)變量
- }
運(yùn)行結(jié)果:
何道晶 中國
中國
2). 千萬不要為了方便訪問數(shù)據(jù)而使用static修飾成員變量,只有成員變量的數(shù)據(jù)真正需要被共享的時(shí)候,才使用static修飾
static修飾成員變量的應(yīng)用場景:如果一個(gè)數(shù)據(jù)需要被所有對(duì)象共享使用的時(shí)候,用static修飾
2.static修飾成員函數(shù)(靜態(tài)的成員方法)
靜態(tài)成員函數(shù)的訪問方式:
方式一:可以使用對(duì)象進(jìn)行訪問
格式:對(duì)象.靜態(tài)的函數(shù)名
方式二:可以使用類名進(jìn)行訪問
格式:類名.靜態(tài)的函數(shù)名
推薦使用類名直接訪問靜態(tài)的成員
原因:
1.方便
2.節(jié)省內(nèi)存
靜態(tài)函數(shù)要注意的事項(xiàng):
1.靜態(tài)函數(shù)是可以調(diào)用類名或者對(duì)象進(jìn)行調(diào)用的,而非靜態(tài)函數(shù)只能使用對(duì)象進(jìn)行調(diào)用
2.靜態(tài)的函數(shù)可以訪問靜態(tài)的成員,但是不能直接訪問非靜態(tài)的成員
3.非靜態(tài)的函數(shù)是可以直接訪問靜態(tài)與非靜態(tài)的成員
4.靜態(tài)函數(shù)不能出現(xiàn)this或者super關(guān)鍵字
- public class Student {
- String name;//姓名 非靜態(tài)成員變量
- //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù)
- static String country = "中國";//國籍 靜態(tài)成員變量
- //構(gòu)造函數(shù)
- public Student(String name) {
- this.name = name;
- }
- //說話行為
- //靜態(tài)成員方法
- public static void speak(){
- System.out.println("國籍:"+country);
- }
- //學(xué)習(xí)行為
- //非靜態(tài)成員方法
- public void study(){
- System.out.println(name+"好好學(xué)習(xí)");
- }
- }
- class Test{
- public static void main(String[] args) {
- Student student = new Student("何道昌");
- System.out.println(student.name);//用對(duì)象訪問非靜態(tài)變量
- System.out.println(student.country);//用對(duì)象訪問靜態(tài)變量
- System.out.println(Student.country);//用類命訪問靜態(tài)變量
- student.study();//用對(duì)象訪問非靜態(tài)方法
- student.speak();//用對(duì)象訪問靜態(tài)方法
- Student.speak();//用類名訪問靜態(tài)方法
- }
- }
運(yùn)行結(jié)果:
何道晶
中國
中國
何道晶好好學(xué)習(xí)
國籍:中國
國籍:中國
靜態(tài)的成員變量與非靜態(tài)的成員變量的區(qū)別:
1)、作用上的區(qū)別:
1.靜態(tài)的成員變量的作用是共享一個(gè)數(shù)據(jù)給所有的對(duì)象使用
2.非靜態(tài)的成員變量的作用是描述一類事物的公共屬性
2)、數(shù)量和存儲(chǔ)位置上的區(qū)別:
1.靜態(tài)成員變量是在存儲(chǔ)方法區(qū)內(nèi)存中,而且只會(huì)存在一份數(shù)據(jù)
2.非靜態(tài)的成員變量是存儲(chǔ)在堆內(nèi)存中,有n個(gè)對(duì)象就有n份數(shù)據(jù)
3)、生命周期的區(qū)別:
1.靜態(tài)的成員變量數(shù)據(jù)是隨著類的加載而存在,隨著類文件的消失而消失
2.非靜態(tài)的成員變量數(shù)據(jù)是隨著對(duì)象的創(chuàng)建而存在,隨著對(duì)象被垃圾回收器回收而消失
靜態(tài)函數(shù)不能訪問非靜態(tài)的成員?
靜態(tài)函數(shù)只要存在有對(duì)象,那么也可以訪問非靜態(tài)的數(shù)據(jù),只是不能直接訪問。
最后,繼續(xù)用這個(gè)例子穿插一下靜態(tài)代碼塊的知識(shí)
靜態(tài)代碼塊是在Student.class文件加載到內(nèi)存的時(shí)候就馬上執(zhí)行的
- public class Student {
- String name;//姓名 非靜態(tài)成員變量
- //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù)
- static String country = "中國";//國籍 靜態(tài)成員變量
- //靜態(tài)代碼塊
- static{
- System.out.println("靜態(tài)代碼塊執(zhí)行了!!");
- }
- //構(gòu)造函數(shù)
- public Student(String name) {
- this.name = name;
- }
- //說話行為
- //靜態(tài)成員方法
- public static void speak(){
- System.out.println("國籍:"+country);
- }
- //學(xué)習(xí)行為
- //非靜態(tài)成員方法
- public void study(){
- System.out.println(name+"好好學(xué)習(xí)");
- }
- }
- class Test{
- public static void main(String[] args) {
- Student.speak();
- }
- }
運(yùn)行結(jié)果:
靜態(tài)代碼塊執(zhí)行了!!
國籍:中國
理解到這,你再看看下面的分析圖,應(yīng)該還會(huì)有所收獲
最后再跟大家分享一句話:
一個(gè)人若想改變自己的命運(yùn),最重要的是要改變自己,改變心態(tài),改變環(huán)境,這樣命運(yùn)也會(huì)隨之改變