• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exception in thread "main" java.lang.StackOverflowError

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class P
{


P p1=new P();
protected P printInfo()
{

System.out.println("Inside class p method");

return p1;

}

}

class Q extends P
{

Q q1=new Q();


public Q printInfo()
{

System.out.println("Inside class q method");
return q1;

}


}

public class override{



public static void main(String[] arg)
{

P p=new P();
Q q=new Q();
P r=new Q();

p=p.printInfo();
q=q.printInfo() ;
r=r.printInfo() ;

}


}


anyone please explain the error.

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Q class, having Q q1=new Q(); is probably not a good idea. This will cause to instanciate Q objects forever. Well, until StackOverflowError is thrown.
 
rohit-kumar Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thankx for the reply.

but what about the P class?

Explain in full detail.

Thanks in advance.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please UseCodeTags when you post source code.

Do you understand what happens if you have code like this:

(you have exactly the same construct in class Q).

What happens when you create a new P object?

1. A new P object is created.
2. The object is initialized. It has a member variable p1, for which a new P object needs to be created.
3. That new P object is created.
4. But it also needs to be initialized. It has its own member variable p1, for which a new P object needs to be created.
5. Go to step 3.

So, there is an infinite recursive loop which keeps running until the stack overflows, and then you get a StackOverflowError.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, UseCodeTags. You can edit your post to add them.

Like Q, class P has an instance field of type P itself that is initialized to a new P object whenever a new P object is created. That means that when you create one P object, you get the following chain of calls:
This ends by the JVM throwing a StackOverflowError.
 
rohit-kumar Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic