public class Test { /**
 * @param args
 */
private Test a;
private Test b= new Test();
public static void main(String[] args) {
// TODO Auto-generated method stub
//Test c =new Test(); //注释掉就对了
}
}
高手解释一下: 和C++ 中的自己引用自己 的区别

解决方案 »

  1.   

    //Test c =new Test(); //注释掉就对了 
    你发生的问题不在这。
      

  2.   

    我好像以前看过这样问题的解释。。
    好像是你的代码中的 private Test b= new Test(); 这一句有问题当在 main 函数中创建 new Test() 时,private Test b= new Test()这一句就会生效,又创建一个Test,
    而新创建的Test又有main,而这个main 函数中又再次创建了 Test ( 因为Test c =new Test() )
    这样就相当于产生了一个 递归。。
    你把private Test b= new Test()改成private final static Test b= new Test() 就ok了,或把这行注释掉。我记得好像是这样解释的,不知道对否,楼主自己甄别。
      

  3.   

    我好像以前看过这样问题的解释。。
    好像是你的代码中的 private Test b= new Test(); 这一句有问题当在 main 函数中创建 new Test() 时,private Test b= new Test()这一句就会生效,又创建一个Test,
    而新创建的Test又有main,而这个main 函数中又再次创建了 Test ( 因为Test c =new Test() )
    这样就相当于产生了一个 递归。。
    你把private Test b= new Test()改成private final static Test b= new Test() 就ok了,或把这行注释掉。我记得好像是这样解释的,不知道对否,楼主自己甄别。