Demonstrates the pitfalls of depending on the order of static initializers : 初始化块 « 类定义 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 类定义 » 初始化块 
5. 11. 10. Demonstrates the pitfalls of depending on the order of static initializers
/*
 *     file: StaticOrderDemo.java
 *  package: oreilly.hcj.review
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */


import java.util.Arrays;

/**  
 * Demonstrates the pitfalls of depending on the order of static initializers.
 *
 @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 @version $Revision: 1.3 $
 */
public class StaticOrderDemo {
  static {
    Class cl = Values.class;
    System.out.println("Class " + cl.getName() " Loaded");
  }

  /** 
   * Creates a new instance of StaticOrderDemo
   */
  public StaticOrderDemo() {
  }

  /** 
   * The main demo method.  Used only to start the virtual machine.
   *
   @param args Arguments passed.
   */
  public static final void main(final String[] args) {
  }

  // --- Inner Classes --

  /**  
   * Holds ranges for the values.
   *
   @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.3 $
   */
  public static class Ranges {
    /** Bluw range */
    public static final String[] RANGE_BLUE = "Sky""Navy" };

    /** Red range */
    public static final String[] RANGE_RED = "Light""Dark" };

    static {
      System.out.println("static{} method for Ranges");
      System.out.println(Arrays.asList(RANGE_BLUE));
      System.out.println(Values.VALUE_SPECIFIER);
      System.out.println(Arrays.asList(RANGE_RED));
    }
  }

  /**  
   * Holds values.
   *
   @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.3 $
   */
  public static class Values {
    /** A value holder */
    public static final String VALUE = "Blue";

    /** A specifier for the value */
    public static final String VALUE_SPECIFIER;

    static {
      System.out.println("static{} method for Values");
      System.out.println(VALUE);
      System.out.println(Ranges.RANGE_BLUE);
      VALUE_SPECIFIER = Ranges.RANGE_BLUE[1];
    }
  }
}

/* ########## End of File ########## */
5. 11. 初始化块
5. 11. 1. 类包含一个静态初始化:
5. 11. 2. 初始化数据成员
5. 11. 3. 初始化顺序
5. 11. 4. Using Initialization Blocks: A non-static initialization block
5. 11. 5. 使用初始化块:静态初始化块
5. 11. 6. 混合初始化
5. 11. 7. 静态初始化的静态条款
5. 11. 8. 全过程的初始化
5. 11. 9. 各种初始化
5. 11. 10. Demonstrates the pitfalls of depending on the order of static initializers
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.