Think in java 答案_Chapter 4_Exercise 17

来源:互联网 发布:java中的权限管理 编辑:程序博客网 时间:2024/06/02 02:26

阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx

/****************** Exercise 17 *****************
* Create a class with a static String field that
* is initialized at the point of definition, and
* another one that is initialized by the static
* block. Add a static method that prints both
* fields and demonstrates that they are both
* initialized before they are used.
***********************************************/

public class E17_StaticStringInitialization {
  static String s1 = "Initialized at definition";
  static String s2;
  static {
    s2 = "Initialized in static block";
  }
  public static void main(String args[]) {
    System.out.println("s1 = " + s1);
    System.out.println("s2 = " + s2);
  }
}

//+M java E17_StaticStringInitialization

**main( ) is a static method, so I just used that to print out the values.
 

原创粉丝点击