這篇文章主要介紹“怎么解決Static坑”,在日常操作中,相信很多人在怎么解決Static坑問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么解決Static坑”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)建站秉承實(shí)現(xiàn)全網(wǎng)價(jià)值營(yíng)銷的理念,以專業(yè)定制企業(yè)官網(wǎng),網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作,重慶小程序開(kāi)發(fā),網(wǎng)頁(yè)設(shè)計(jì)制作,手機(jī)網(wǎng)站制作,全網(wǎng)整合營(yíng)銷推廣幫助傳統(tǒng)企業(yè)實(shí)現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級(jí)專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對(duì)客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長(zhǎng)。
如果沒(méi)有static會(huì)怎樣?
需求:
1、定義Student類
姓名、國(guó)籍,說(shuō)話行為
多個(gè)構(gòu)造,重載形式體現(xiàn)
2、學(xué)生的國(guó)籍都是確定的
國(guó)籍可以進(jìn)行顯示初始化
public class Student { String name;//姓名 String country;//國(guó)籍 public Student(String name, String country) { this.name = name; this.country = country; } public void speak(){ System.out.println("姓名:"+this.name+" "+"國(guó)籍:"+this.country); } } class Test{ public static void main(String[] args) { Student student = new Student("何道昌","中國(guó)"); Student student1 = new Student("孫雙雙","中國(guó)"); student.speak(); student1.speak(); } }
運(yùn)行結(jié)果:
姓名:何晶晶 國(guó)籍:中國(guó)
姓名:孫雙雙 國(guó)籍:中國(guó)
目前存在的問(wèn)題:
現(xiàn)在我們已知學(xué)生都是中國(guó)人,現(xiàn)在我們每創(chuàng)建一個(gè)學(xué)生對(duì)象,就要給所有學(xué)生的國(guó)籍屬性賦相同的值,這樣造成堆內(nèi)存空間資源浪費(fèi)
目前方案:
把“中國(guó)”這個(gè)數(shù)據(jù)移動(dòng)到數(shù)據(jù)共享區(qū)中,共享這個(gè)數(shù)據(jù)給所有的Student對(duì)象使用即可
疑問(wèn):如何才能把這個(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 = "中國(guó)";//國(guó)籍 //構(gòu)造函數(shù) public Student(String name) { this.name = name; } //說(shuō)話行為 public void speak(){ System.out.println("姓名:"+this.name+" "+"國(guó)籍:"+country); } } class Test{ public static void main(String[] args) { Student student = new Student("何道昌"); Student student1 = new Student("孫雙雙"); student.speak(); student1.speak(); } }
運(yùn)行結(jié)果:
姓名:何晶晶 國(guó)籍:中國(guó)
姓名:孫雙雙 國(guó)籍:中國(guó)
下面我來(lái)詳細(xì)解說(shuō)static
static(靜態(tài)修飾符)
1.static修飾靜態(tài)變量
如果有數(shù)據(jù)需要被共享給所有對(duì)象使用時(shí),那么就可以使用static修飾
靜態(tài)成員變量的訪問(wèn)方式:
方式一:可以使用對(duì)象進(jìn)行訪問(wèn)
格式:對(duì)象.變量名
方式二:可以使用類名進(jìn)行訪問(wèn)
格式:類名.變量名
注意:
1). 非靜態(tài)的成員變量只能使用對(duì)象進(jìn)行訪問(wèn),不能使用類命進(jìn)行訪問(wèn)
public class Student { String name;//姓名 非靜態(tài)成員變量 //使用了static修飾country,那么這時(shí)候country就是一個(gè)共享的數(shù)據(jù) static String country = "中國(guó)";//國(guó)籍 靜態(tài)成員變量 //構(gòu)造函數(shù) public Student(String name) { this.name = name; } //說(shuō)話行為 public void speak(){ System.out.println("姓名:"+this.name+" "+"國(guó)籍:"+country); } } class Test{ public static void main(String[] args) { Student student = new Student("何道昌"); System.out.println(student.name);//用對(duì)象訪問(wèn)非靜態(tài)變量<br> Systen.out.println(student.country);//用對(duì)象訪問(wèn)靜態(tài)變量 System.out.println(Student.country);//用類命訪問(wèn)靜態(tài)變量 }
運(yùn)行結(jié)果:
何道晶 中國(guó)
中國(guó)
2). 千萬(wàn)不要為了方便訪問(wèn)數(shù)據(jù)而使用static修飾成員變量,只有成員變量的數(shù)據(jù)真正需要被共享的時(shí)候,才使用static修飾
static修飾成員變量的應(yīng)用場(chǎng)景:如果一個(gè)數(shù)據(jù)需要被所有對(duì)象共享使用的時(shí)候,用static修飾
2.static修飾成員函數(shù)(靜態(tài)的成員方法)
靜態(tài)成員函數(shù)的訪問(wèn)方式:
方式一:可以使用對(duì)象進(jìn)行訪問(wèn)
格式:對(duì)象.靜態(tài)的函數(shù)名
方式二:可以使用類名進(jìn)行訪問(wèn)
格式:類名.靜態(tài)的函數(shù)名
推薦使用類名直接訪問(wèn)靜態(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ù)可以訪問(wèn)靜態(tài)的成員,但是不能直接訪問(wèn)非靜態(tài)的成員
3.非靜態(tài)的函數(shù)是可以直接訪問(wèn)靜態(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 = "中國(guó)";//國(guó)籍 靜態(tài)成員變量 //構(gòu)造函數(shù) public Student(String name) { this.name = name; } //說(shuō)話行為 //靜態(tài)成員方法 public static void speak(){ System.out.println("國(guó)籍:"+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ì)象訪問(wèn)非靜態(tài)變量 System.out.println(student.country);//用對(duì)象訪問(wèn)靜態(tài)變量 System.out.println(Student.country);//用類命訪問(wèn)靜態(tài)變量 student.study();//用對(duì)象訪問(wèn)非靜態(tài)方法 student.speak();//用對(duì)象訪問(wèn)靜態(tài)方法 Student.speak();//用類名訪問(wèn)靜態(tài)方法 } }
運(yùn)行結(jié)果:
何道晶
中國(guó)
中國(guó)
何道晶好好學(xué)習(xí)
國(guó)籍:中國(guó)
國(guó)籍:中國(guó)
靜態(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ù)不能訪問(wèn)非靜態(tài)的成員?
靜態(tài)函數(shù)只要存在有對(duì)象,那么也可以訪問(wèn)非靜態(tài)的數(shù)據(jù),只是不能直接訪問(wèn)。
最后,繼續(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 = "中國(guó)";//國(guó)籍 靜態(tài)成員變量 //靜態(tài)代碼塊 static{ System.out.println("靜態(tài)代碼塊執(zhí)行了??!"); } //構(gòu)造函數(shù) public Student(String name) { this.name = name; } //說(shuō)話行為 //靜態(tài)成員方法 public static void speak(){ System.out.println("國(guó)籍:"+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í)行了!!
國(guó)籍:中國(guó)
理解到這,你再看看下面的分析圖,應(yīng)該還會(huì)有所收獲
到此,關(guān)于“怎么解決Static坑”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
當(dāng)前題目:怎么解決Static坑
鏈接分享:http://chinadenli.net/article42/pijcec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站收錄、動(dòng)態(tài)網(wǎng)站、服務(wù)器托管、品牌網(wǎng)站制作、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)