欢迎来到天天文库
浏览记录
ID:51690229
大小:31.51 KB
页数:3页
时间:2020-03-15
《Java中静态变量的适用场景.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Java中静态变量的适用场景 Java类中的静态变量在程序运行期间,其内存空间对所有该类的对象实例而言是共享的,有些时候可以认为是全局变量。因此在某些时候为了节省系统内存开销、共享资源,可以将类中的一些变量声明为静态变量,通过下面的例子,你可以发现合理应用静态变量带来的好处: Java代码 publicclassWeekA{ staticclassData{ privateintweek; privateStringname; Data(inti,Strings){ week=i; name=s
2、; } } Dataweeks[]={ newData(1,"Monday"),newData(2,"Tuesay"),newData(3,"Wednesday"),newData(4,"Thursday"),newData(5,"Friday"),newData(6,"Saturday"),newData(7,"Sunday") }; publicstaticvoidmain(Stringargs[]){ finalintN=10000; WeekAweekinstance; for(inti
3、=1;i<=N;i++){ weekinstance=newWeekA(); } } } publicclassWeekA{ staticclassData{ privateintweek; privateStringname; Data(inti,Strings){ week=i; name=s; } } Dataweeks[]={ newData(1,"Monday"),newData(2,"Tuesay"),newData(3,"Wednesday"),newData(4,"T
4、hursday"),newData(5,"Friday"),newData(6,"Saturday"),newData(7,"Sunday") }; publicstaticvoidmain(Stringargs[]){ finalintN=10000; WeekAweekinstance; for(inti=1;i<=N;i++){ weekinstance=newWeekA(); } } }
此文档下载收益归作者所有