Java中靜態(tài)變量的適用場(chǎng)景
Java類(lèi)中的'靜態(tài)變量在程序運(yùn)行期間,其內(nèi)存空間對(duì)所有該類(lèi)的對(duì)象實(shí)例而言是共享的,有些時(shí)候可以認(rèn)為是全局變量。因此在某些時(shí)候?yàn)榱斯?jié)省系統(tǒng)內(nèi)存開(kāi)銷(xiāo)、共享資源,可以將類(lè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 ();
}
}
}
【Java中靜態(tài)變量的適用場(chǎng)景】相關(guān)文章:
php靜態(tài)成員變量分享11-25
Java中import的使用09-21
Java中接口的使用09-30