Java中靜態變量的適用場景
Java類中的'靜態變量在程序運行期間,其內存空間對所有該類的對象實例而言是共享的,有些時候可以認為是全局變量。因此在某些時候為了節省系統內存開銷、共享資源,可以將類中的一些變量聲明為靜態變量,通過下面的例子,你可以發現合理應用靜態變量帶來的好處:
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中靜態變量的適用場景】相關文章:
php靜態成員變量分享11-25
php中static靜態變量的使用方法11-16
Java中import的使用09-21
Java中接口的使用09-30
CSS中關于變量的基本教程詳解11-20
動畫場景設計中的色彩應用09-19
Java中的動態代碼編程06-05
Java中goto實現方法09-24