JAVA认证

Java中静态变量的适用场景

时间:2024-07-16 20:11:16 JAVA认证 我要投稿
  • 相关推荐

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中static静态变量的使用方法05-25

php静态成员变量分享06-21

静态化网站在SEO中的优势08-20

如何使用网络中静态IP地址10-02

CSS中关于变量的基本教程详解07-12

在java中Synchronized的用法09-03

在Java中执行JavaScript代码07-03

Java中调用Oracle包的过程10-16

PHP的变量的介绍08-15

关于Java中Queue和BlockingQueue的区别08-28