一个简单的程序,编译可以通过但是,不出现结果,错误提示为无数个at   Test. <init> (TestNonStatic.java:8), class   Test   { 
    Test()   {} 
    Test   t=   new   Test(); 
    int   fact(int   n)   { 
        if(n> 0)   { 
            return   t.fact(n-1)*n;//递归调用类Test内的fact函数 
        }         
        else   { 
            return   1; 
        }     
    } 

public   class   TestNonStatic   { 
    public   static   void   main(String[]   args)   { 
        Test   t1=new   Test(); 
        int   m=t1.fact(5); 
        System.out.println(m); 
    }   
}  

解决方案 »

  1.   

    楼主多此一举了。(Test t=new Test();)
      

  2.   

    Test类里面又有一个Test t = new Test()
    导致new Test()时无穷递归最后栈溢出了
      

  3.   

    class Test {  
      Test() {}  
      Test t= new Test();  
      int fact(int n) {  
      if(n> 0) {  
      return t.fact(n-1)*n;//递归调用类Test内的fact函数  
      }   
      else {  
      return 1;  
      }   
      }  
    }  
    public class TestNonStatic {  
      public static void main(String[] args) {  
      Test t1=new Test();  
      int m=t1.fact(5);  
      System.out.println(m);  
      }