3
public class testing {    
    testing t=new testing();                

    public static void main(String args[]){   
        testing t1=new testing();  
        t1.fun();  
    }

    void fun(){         
        int a=2;        
        int b=t.fun2(a);  
        System.out.println(a+" "+b);  
    }

    int fun2(int a)  
    {
        a=3;  
        return a;  
    }  
}

why does above code giving following error? I just want to know the reason because it is difficult to expect StackOverFlowError error in this case.

Exception in thread "main" java.lang.StackOverflowError
at com.testing.<init>(testing.java:4)
at com.testing.<init>(testing.java:4)
4
  • 1
    Ahh, please indent your code. It's much easier to help you when your code is readable. Commented Jun 27, 2012 at 5:32
  • 1
    Also, classes should be capitalized. Look up some code style guidelines. Commented Jun 27, 2012 at 5:34
  • 1
    @jack57 I know coding conventions, just written the code for quick testing. But thanx for advice anyways. Commented Jun 27, 2012 at 5:39
  • can someone provide a link where I can refer to understand because I still have some doubts. Commented Jun 27, 2012 at 6:55

3 Answers 3

13

You are recursively creating instance of testing

public class testing {    
 testing t=new testing();        
//

}

While creating first instance it will create new Instance by testing t=new testing(); which will again create new instance and so on

Sign up to request clarification or add additional context in comments.

10 Comments

I created the object for class testing and according to my understanding it will be created only once. Can you explain how multiple instances are creating?
While creating first instance it will create new Instance by testing t=new testing(); which will again create new instance and so on
when you create the new testing() first time it is creating a object of that class but that class has new testing() and create that object and it will continue..
@JigarJoshi Still i don't understant as public class testing { testing t=new testing(); public static void main(String args[]){ System.Out.Print("hello");}} runs successfuly.
@user980089 What's happening is that you instantiate a testing object, and the first thing that happens is that this object runs all of its field initializers. In your case, the only initializer is testing t = new testing(), so that runs. Well, what's that do? It instantiates a testing object, which means all of its fields get initialized -- namely, its testing t = new testing(). And so on. But without that first new testing() to get things started, none of them run -- because testing t is an instance variable, not a static variable, and thus only applies to new instances.
|
1

try this solution,

public class testing {                

    public static void main(String args[]){   
        testing t1=new testing();  
        t1.fun(t1);  
    }

    void fun(testing t1){         
        int a=2;        
        int b=t1.fun2(a);  
        System.out.println(a+" "+b);  
    }

    int fun2(int a)  
    {
        a=3;  
        return a;  
    }  
}

1 Comment

thanx kushan for solution, but i just want to know the reason for error.
1

You have to create field at class level but instantiate it once where in main

public class Testing {    
    static Testing t1;              

    public static void main(String args[]){   
        t1=new Testing();  
        t1.fun();  
    }

    void fun(){         
        int a=2;        
        int b=t1.fun2(a);  
        System.out.println(a+" "+b);  
    }

    int fun2(int a)  
    {
        a=3;  
        return a;  
    }  
}

1 Comment

some alternate solutions are also there which are able to elude that problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.