当你new Sub()时,System.out.println(""); 也会被当作private String str;一样被先运行的

解决方案 »

  1.   

    new Sub(); 
    System.out.println("");被运行了!
      

  2.   

    to:cool_li
    你也喜欢麻辣教师吗,我很喜欢阿
      

  3.   

    类的里面的东西都要实例化才能用,除非是Static的 方法或字段可以直接用"类名.方法名[字段名]" 来调用
      

  4.   

    我的意思是说当类被载入时,它是不是就运行了
    比如说class.forName();
      

  5.   

    而不一定是在new sub()时才运行
      

  6.   

    《Java Language SpecificationSecond Edition》
    8.7 Static Initializers 
    Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers (§8.3.2) for class variables, may be used to initialize the class variables of the class (§12.4).
    StaticInitializer:
     static Block
    It is a compile-time error for a static initializer to be able to complete abruptly (§14.1, §15.6) with a checked exception (§11.2). It is a compile-time error if a static initializer cannot complete normally (§14.20).
    The static initializers and class variable initializers are executed in textual order. 
    Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.2.3 for the precise rules governing forward reference to class variables.
    If a return statement (§14.16) appears anywhere within a static initializer, then a compile-time error occurs.
    If the keyword this (§15.8.3) or the keyword super (§15.11, §15.12) appears anywhere within a static initializer, then a compile-time error occurs
      

  7.   

    太感谢你了,老木,我怎么没想到去找spec,多谢多谢
      

  8.   

    我想起来了,以前遇到过
    static{
    }块
    那么是不是 不加static
    就只有 调用构造函数时才会被运行啊
    而加了static 在类被初始化的时候就运行了