• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why its So ?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body tell whats happening here

class SCJP
{
SCJP()
{
System.out.println("From SCJP Cons");
}
}

public class SCJPTest extends SCJP
{
SCJPTest s1 = new SCJPTest();
public static void main(String[] agrs)
{
SCJPTest s = new SCJPTest();
System.out.println("This will not excute ?");
}
}

When i run it showing stack overflow due to line 11 (SCJPTest s1 = new SCJPTest()
 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




main() creates a new SCJPTest instance. When member s1 of this instance is initialized, a new SCJPTest instance is created. Thus you end up with an infinite loop of object creation which continues till the overflow occurs.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the result will be:

from SCJP Cons
This will not excute ?
 
Narendra Jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx Jack Ryan,

But can you explain me what makes it to overflow how control is flowing in the program. Am i calling the main() again and again ?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jack,
I did not understand your explanation, can you please explain me clearly.
regards,
sri.
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you do a 'new SCJPTest()', it creates a new instance of SCJPTest and then initializes its member s1 by creating a new instance of SCJPTest
as s1 = new SCJPTest();

As you can see, towards the end, the flow doesn't stop, it rather restarts itself with the call 'new SCJPTest()' which explains why the control would never come out of this 'mechanism'.

StackOverflowError is thrown when the application recurses too deeply, as is the case here.
[ January 03, 2006: Message edited by: Jack Ryan ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I am not clear why is this happening.
something to do with class loading and constructor call
My thinking is
1. When the constructor of child class gets called from the main function.base class gets loaded
2.Constructor of Super class gets call and it intilise super class and returns
3. flow comes back to base class and tris to intilise base class object,which it is never able to do because of re-invocation of constructor.

Call to constructor of any class complets ,after intialisation of Object successfuy,But in this case since the intialisation of objet never gets completed so, the constructor never completes and program gets into recursion.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

this will go in a infinite loop
reason : the statement in main()

IOTest s = new IOTest(); will call the constructor in super class then it will call the initialized block
IOTest s1= new IOTest();
statement.

and will again call the construcor in super class and so on...

thus its a infinite loop
mohit
singhalkmohit@gmail.com
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vivek,

Your explanation is quite right in the context of the posted code. But the same behaviour can be observed with the below posted sample code too. It has nothing to do with Inheritance. You are right in stating that :

"Call to constructor of any class completes ,after intialisation of Object successfuy,But in this case since the intialisation of objet never gets completed so, the constructor never completes and program gets into recursion."

Simple code that shows the same behaviour with the inheritance:

public class SimpleClass {
SimpleClass simple = new SimpleClass();
public static void main(String[] args) {
new SimpleClass();
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"bobgalli s,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. The first can be an initial, but the last cannot.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
reply
    Bookmark Topic Watch Topic
  • New Topic