各种初始化 : 初始化块 « 类定义 « 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. 9. 各种初始化
/*
 *     file: InitializerDemo.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.StringTokenizer;

/**  
 * Demonstrates various initializers.
 *
 @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 @version $Revision: 1.4 $
 */
public class InitializerDemo {
  /** Simple static initialization. */
  public static final String NAME = "Initializer Demo";

  /** Initialized static on one line. */
  public static final String ARCH = System.getProperty("os.arch");

  /** Static method based initialization. */
  public static final String USER_HOME;

  static {
    USER_HOME = System.getProperty("user.home");
  }

  /** Simple instance member initialization. */
  public String description = "An initialized member";

  /** Method call instance member initialization. */
  public long timestamp = System.currentTimeMillis();

  /** Complex instance member initialization. */
  private String xmlClasspath;
  {
    final StringBuffer buf = new StringBuffer(500);
    final String classPath = System.getProperty("java.class.path");
    StringTokenizer tok =
      new StringTokenizer(classPath, System.getProperty("path.separator"));
    buf.append("<classpath>\n");
    while (tok.hasMoreTokens()) {
      buf.append("  <pathelement location=\"");
      buf.append(tok.nextToken());
      buf.append("\"/>\n");
    }
    buf.append("</classpath>");
    xmlClasspath = buf.toString();
  }

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

  /** 
   * Main method of the demonstration.
   *
   @param args Command line arguments (ignored).
   */
  public static final void main(final String[] args) {
    InitializerDemo demo = new InitializerDemo();
    System.out.println("------Dumping Contents-----------");
    System.out.println("---------------------------------");
    System.out.println(InitializerDemo.NAME);
    System.out.println(InitializerDemo.ARCH);
    System.out.println(InitializerDemo.USER_HOME);
    System.out.println(demo.description);
    System.out.println(demo.xmlClasspath);
    System.out.println("---------------------------------");
  }
}

/* ########## 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.