在线亚洲男人的天堂a∨_中国毛片区免费区_伊人久久中文大香线蕉综合_久了精品国产99国产精

將想法與焦點(diǎn)和您一起共享

Java中靜態(tài)變量的適用場(chǎng)景發(fā)布者:本站     時(shí)間:2020-05-06 15:05:06

Java類中的靜態(tài)變量在程序運(yùn)行期間,其內(nèi)存空間對(duì)所有該類的對(duì)象實(shí)例而言是共享的,有些時(shí)候可以認(rèn)為是全局變量。因此在某些時(shí)候?yàn)榱斯?jié)省系統(tǒng)內(nèi)存開銷、共享資源,可以將類中的一些變量聲明為靜態(tài)變量,通過(guò)下面的例子,你可以發(fā)現(xiàn)合理應(yīng)用靜態(tài)變量帶來(lái)的好處:

Java代碼


public class WeekA{
static class Data {
private int week;
private String name;
Data(int i, String s) {
week= i;
name = s;
}
}
Data weeks[] = {
new Data(1, "Monday"), new Data(2, "Tuesay"), new Data(3, "Wednesday"), new Data(4, "Thursday"), new Data(5, "Friday"), new Data(6, "Saturday"), new Data(7, "Sunday")
};
public static void main(String args[]) {
final int N = 10000;
WeekA weekinstance;
for (int i = 1; i <= N; i++){
weekinstance = new WeekA ();
}
}
}
public class WeekA{
static class Data {
private int week;
private String name;
Data(int i, String s) {
week= i;
name = s;
}
}
Data weeks[] = {
new Data(1, "Monday"), new Data(2, "Tuesay"), new Data(3, "Wednesday"), new Data(4, "Thursday"), new Data(5, "Friday"), new Data(6, "Saturday"), new Data(7, "Sunday")
};
public static void main(String args[]) {
final int N = 10000;
WeekA weekinstance;
for (int i = 1; i <= N; i++){
weekinstance = new WeekA ();
}
}
}


在上面這段代碼中,沒有將Data weeks聲明為靜態(tài)變量,因此當(dāng)創(chuàng)建WeekA對(duì)象時(shí)將會(huì)得到10 000個(gè)weeks對(duì)象的副本,這些對(duì)象被保存在內(nèi)存中,但是weeks對(duì)象中的數(shù)據(jù)卻從來(lái)沒有被更改過(guò),而且十分穩(wěn)定。因此,如果能使所有對(duì)象共享該數(shù)據(jù)對(duì)象是個(gè)不錯(cuò)的解決辦法,請(qǐng)看下面的代碼:
Java代碼


public class WeekB{
static class Data {
private int week;
private String name;
Data(int i, String s) {
week= i;
name = s;
}
}
static Data weeks[] = {
new Data(1, "Monday"), new Data(2, "Tuesay"), new Data(3, "Wednesday"), new Data(4, "Thursday"), new Data(5, "Friday"), new Data(6, "Saturday"), new Data(7, "Sunday")
};
public static void main(String args[]) {
final int N = 10000;
WeekB weekinstance;
for (int i = 1; i <= N; i++){
weekinstance = new WeekB ();
}
}
}
public class WeekB{
static class Data {
private int week;
private String name;
Data(int i, String s) {
week= i;
name = s;
}
}
static Data weeks[] = {
new Data(1, "Monday"), new Data(2, "Tuesay"), new Data(3, "Wednesday"), new Data(4, "Thursday"), new Data(5, "Friday"), new Data(6, "Saturday"), new Data(7, "Sunday")
};
public static void main(String args[]) {
final int N = 10000;
WeekB weekinstance;
for (int i = 1; i <= N; i++){
weekinstance = new WeekB ();
}
}
}


請(qǐng)注意在類WeekB中,在Data weeks[]之前添加了static關(guān)鍵字,將該對(duì)象變量聲明為靜態(tài)的,因此當(dāng)你創(chuàng)建10 000個(gè)WeekB對(duì)象時(shí)系統(tǒng)中只保存著該對(duì)象的一份拷貝,而且該類的所有對(duì)象實(shí)例共享這份拷貝,這無(wú)疑節(jié)約了大量的不必要的內(nèi)存開銷,同時(shí)實(shí)現(xiàn)了要完成的系統(tǒng)功能。

那么是不是我們應(yīng)該盡量地多使用靜態(tài)變量呢?其實(shí)不是這樣的,因?yàn)殪o態(tài)變量生命周期較長(zhǎng),而且不易被系統(tǒng)回收,因此如果不能合理地使用靜態(tài)變量,就會(huì)適得其反,造成大量的內(nèi)存浪費(fèi),所謂過(guò)猶不及。因此,建議在具備下列全部條件的情況下,盡量使用靜態(tài)變量:

(1)變量所包含的對(duì)象體積較大,占用內(nèi)存較多。

(2)變量所包含的對(duì)象生命周期較長(zhǎng)。

(3)變量所包含的對(duì)象數(shù)據(jù)穩(wěn)定。

(4)該類的對(duì)象實(shí)例有對(duì)該變量所包含的對(duì)象的共享需求。

如果變量不具備上述特點(diǎn)建議你不要輕易地使用靜態(tài)變量,以免弄巧成拙。



選擇我們,優(yōu)質(zhì)服務(wù),不容錯(cuò)過(guò)
1. 優(yōu)秀的網(wǎng)絡(luò)資源,強(qiáng)大的網(wǎng)站優(yōu)化技術(shù),穩(wěn)定的網(wǎng)站和速度保證
2. 15年上海網(wǎng)站建設(shè)經(jīng)驗(yàn),優(yōu)秀的技術(shù)和設(shè)計(jì)水平,更放心
3. 全程省心服務(wù),不必?fù)?dān)心自己不懂網(wǎng)絡(luò),更省心。
------------------------------------------------------------
24小時(shí)聯(lián)系電話:021-58370032