同工程同包下,三个类 第一个  
public class ty {
static int a=dwt.re( );

public static int ty( ) {
return a;
}

}
第二个
public class dwt {
static int b=ty.a;

public static int dwt( ) {
return b;
}

public static int re( ) {
return 1;
}

}第三个
public class qudo {
public static void main(String[] args) {
System.out.println( ty.ty() ); System.out.println( dwt.dwt() );
}
}
运行结果为 :1与0。将dwt.re()方法换换成ty.re()
完整程序如下 第一个类
public class ty {
static int a=re();

public static int ty( ) {
return a;
}

public static int re( ) {
return 1;
}

}
第二个类
public class dwt {
static int b=ty.a;

public static int dwt( ) {
return b;
}

}
第三个类
public class qudo {
public static void main(String[] args) {
System.out.println( ty.ty() ); System.out.println( dwt.dwt() );
}
}
运行结果为 1和1
程序很简单,也没实际意义,但我希望知道为什么会是这样的结果,它说明了类加载时的一个什么问题。谢谢各位大虾

解决方案 »

  1.   

    先帮楼主重新贴代码吧
    public class ty { 
    static int a=re();  public static int ty( ) { 
    return a; 
    }  public static int re( ) { 
    return 1; 
    } } public class dwt { 
    static int b=ty.a;  public static int dwt( ) { 
    return b; 

    } public class qudo {
    public static void main(String[] args) { 
    System.out.println( ty.ty() ); 
    System.out.println( dwt.dwt() ); 
    } }
      

  2.   

    不好意思,再贴一下public class ty { 
    static int a=re();  public static int ty( ) { 
    return a; 
    }  public static int re( ) { 
    return 1; 
    } }public class dwt { 
    static int b=ty.a;  public static int dwt( ) { 
    return b; 

    } public class qudo {
    public static void main(String[] args) { 
    System.out.println( ty.ty() ); 
    System.out.println( dwt.dwt() ); 
    } }
      

  3.   

    然后,为什莫结果是1和1呢
    ty.ty() 
    这句话执行的时候,首先是ty类的a静态变量被re方法赋值为1
    然后,ty()方法又返回a,所以打印的是1同理
    dwt.dwt() 的时候
    首先是dwt类的b变量被赋值为1
    然后,dwt( )方法返回1
    所以打印的是1
      

  4.   

    一步一步看
    1.执行这一步的时候ty.ty()。返回的是a,而a=dwt.re()所以re方法返回1.
    2.执行dwt.dwt()返回的是b,而b=ty.a,而a=dwt.re()所以re方法返回1.
    输出1和1。
      

  5.   

    特别感谢lovecj6185的热心,向你学习,谢谢。