- 相關(guān)推薦
Java中靜態(tài)變量的適用場(chǎng)景
Java類中的靜態(tài)變量在程序運(yùn)行期間,其內(nèi)存空間對(duì)所有該類的對(duì)象實(shí)例而言是共享的,有些時(shí)候可以認(rèn)為是全局變量。因此在某些時(shí)候?yàn)榱斯?jié)省系統(tǒng)內(nèi)存開銷、共享資源,可以將類中的一些變量聲明為靜態(tài)變量,通過下面的例子,你可以發(fā)現(xiàn)合理應(yīng)用靜態(tà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)成員變量分享06-21
靜態(tài)化網(wǎng)站在SEO中的優(yōu)勢(shì)08-20
如何使用網(wǎng)絡(luò)中靜態(tài)IP地址10-02
在Java中執(zhí)行JavaScript代碼07-03
PHP的變量的介紹08-15