Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: stackoverflow

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default stackoverflow

    This might be a very basic question but still I am not able to figure out why is the foll code giving stackoverflow exception in main??

    public class HelloWorld{
         public static void main(String []args)
         {
            System.out.println("Hello World");
            Animal c = new Animal();
         }
    } 
    class Animal
    {
    Animal e = new Animal();
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: stackoverflow

    class Animal
    {
       Animal e = new Animal();
    }

    Looks like a recursive call. Each new Animal creates another new Animal which creates another etc until stack is full.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    peter_parker (March 29th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: stackoverflow

    Quote Originally Posted by Norm View Post
    class Animal
    {
       Animal e = new Animal();
    }

    Looks like a recursive call. Each new Animal creates another new Animal which creates another etc until stack is full.
    Thank you sir actually I sort of knew the reason but was not sure..

Similar Threads

  1. Recursion Stackoverflow Error help!
    By Peopshu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 28th, 2012, 11:25 AM
  2. StackOverFlow Error
    By ankiit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 13th, 2012, 09:49 AM
  3. BlinkingLabel-StackOverflow Error
    By MichaelWU in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 09:25 AM
  4. Why am I getting StackOverflow errors?
    By techcom0 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2011, 09:18 AM