- 相關(guān)推薦
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)成員變量分享05-05
靜態(tài)化網(wǎng)站在SEO中的優(yōu)勢(shì)05-12
如何使用網(wǎng)絡(luò)中靜態(tài)IP地址09-14
在Java中執(zhí)行JavaScript代碼07-14
Java中調(diào)用Oracle包的過(guò)程06-27
PHP的變量的介紹11-06