Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:29:49+00:00 2026-06-06T11:29:49+00:00

Consider following 2 programs giving same error First calss: public class Testing { Testing

  • 0

Consider following 2 programs giving same error
First calss:

public class Testing {    
    Testing t=new Testing();        
    public static void main(String args[]){   
        testing t1=new testing();  
    }
}

Second class:

class foo{
    void baz(){
        new testing();
    }
}
public class testing {
    testing t=new testing();    
    public static void main(String args[]){
    foo f=new foo();
        f.baz();
    }       
}

how does above code giving following error?
I know instance of class is creating recursively but I want to know how?

Exception in thread "main" java.lang.StackOverflowError
    at com.Testing.<init>(Testing.java:4)
    at com.Testing.<init>(Testing.java:4)

also Why doesn’t this happen if we do

    public class testing { 
    testing t2=new testing(); 
    testing t1=new testing(); 
    public static void main(String args[])
    {//anything}
    }

as t1 will require t2 object to be initalized and vice-versa?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:29:50+00:00Added an answer on June 6, 2026 at 11:29 am

    This is the problem:

    public class Testing {    
        Testing t=new Testing();  
    }
    

    This is broadly the same as:

    public class Testing {
        Testing t;
    
        public Testing() {
            t = new Testing();
        }
    }
    

    That makes the recursion clearer – the constructor is unconditionally calling itself.

    Each instance of Testing contains a reference to another instance of Testing. That needn’t be a problem – but it is here, because it’s creating a new instance of Testing. So creating the “outermost” instance immediately triggers the creation of another one… which triggers the creating of another one, etc.

    Your two classes aren’t significantly different in that respect – creating any instance of Testing will cause a stack overflow.

    EDIT: Regarding this code (names and formatting improved):

    public class Testing { 
        Testing t1 = new Testing(); 
        Testing t2 = new Testing(); 
    
        public static void main(String[] args) {
            //anything
        }
    }
    

    will still throw the same exception if you try to create an instance of Testing. You’re wrong to state that “t1 will require t2 object to be initialized” though – there’s no interdependence between the variables here. Each variable initializer creates a separate object… but each of those objects would in turn try to create two more instances of Testing.

    Note that an instance of Testing is not required in order to run the main method – but if that main method either calls new Testing directly, or calls something else which calls new Testing(), then you’ll get the stack overflow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following program: class A { public static void Foo() { } }
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Consider the following Safe program: internal class Safe { public static void SafeMethodWillNeverThrow() {
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +
consider the following program: class Base { public: virtual void foo() const { cout
Consider the following program: import java.util.List; import java.util.ArrayList; public class TypeTest { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.