• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

can any one explain the output

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was working on the following code and got surprised with the output

public class Test {
int i=89;
ram k=new ram();
public void disp()
{
System.out.println("BABAwq111231`11121212213");
}
private class ram extends Test
{

public void disp()
{
System.out.println(i);
}
}


static public void main(String[] args)
{

new Test();

}






}

The output was

Exception in thread "main" java.lang.StackOverflowError

Please explain the cause ?? "i suspect the constructor invocation " but bit confussed........
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you instanciate Test, the JVM initializes its properties, amongst which the "k" property of type "ram".

"Ram" being a subtype of Test, the constructor of the ram instance calls the constructor of its supertype, of type Test. During that supertype initialization, the supertype's "k" property, of type "ram", is initialized... and then you have an infinite loop.

Test initializes a ram instance, that initializes a Test instance, that init... etc.


(By the way, don't hesitate co correct my English if necessary, I'm not very sure of that "amongst" word...)
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yogesh,

you are getting the eroor because of the recursive call. Let me explain,

1. An object for Test is created which contains the variable i and the object for ram.
2 Since the ram extends Test, the attribute of i and the object creation for ram will be inherited to the ram class(which contains again i=89 and Test k=new Test())
3 . So call for Test object creation happens again which in turn cals ram where again Test object creation called again and this happens continously still stack gets overflow.

I thing my resolution is correct. Looking forward for further commands.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Oliver's resolution is the correct one.

Some content firts:

A) When an object is being created, its attributes need to be initialized.
B) Always one a new instance is created all the supertypes constructors are called.

Now the problem:

1. The attribute k is marked to be initialized with a new ram object.
2. So, to create a Test object we need to create a ram object to initialize k variable.
3. And to create a ram object we need to call Test's constructor.

So, we have this situation: to call Test's constructor we need to call ram's constructor and vice-versa. Those steps are repeated over and over until we reach a java.lang.StackOverflowError.

Edit: when I said in item B that all the supertypes constructors are called of course I meant that one constructor of each supertype is called.
[ July 05, 2007: Message edited by: Rafael Souza ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps easier to see here, same structure


-> eternal loop

Yours,
Bu.
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply guys
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From next time, Use code tags.
You simply keep your code inside the <!-- --> tags.
I have commented out the tags above. delete them when you post.
 
You get good luck from rubbing the belly of a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic