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