• 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

Flow of the code snippet

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The following code is throwing "java.lang.StackOverflowError" error.

public class Test
{
public static void main(String[] args)
{
a aa = new b();
}
}
class a{
b f = new b();
void abc() {
System.out.println("aaaa");
}
}
class b extends a {
void abc() {
System.out.println("bbbb");
}
}

Can any one explain why this code is failing with above error.
Thanks in advance...
Deepak Vernekar
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi deepak!!!
here class test contains the main method so first the control comes to the class test where it is asked to creat an object of class b and store the adress of the object in class a's refrence. but when the jvm tries to create object of the class b first it has to create another object of class a since class b inherits class a but when it tries to create an object of class a again that contains a statement to creat an object of class b. so this becomes a recursive procedure and inturn the stack get overflowed....
this is wat it happens i guess!!!


actually when an object of a class is created.. if that class inherits some other class then first the base class object is created.
Actually an Object creation consists of 4 steps internally
1.Allocation of memory in HEAP depending on how many data members that class has
2.All the data members of that object gets default values
3.if there is any inplace intialization then that happens
4.call to constructor.

i hope this helps.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"deepak"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Deepak Kumar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashok...
I think that explains my querry.

I have some thing about initialization of data member.

Why final variables are allowed to initialize in a constructor.. but not other place(like methods).

Thanks,
Deepak Vernekar
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak...

have a look...over here..maybe it is helpful..to you..

https://coderanch.com/t/250972/java-programmer-SCJP/certification/Final-Variable

maybe you can derive some useful points here...

 
reply
    Bookmark Topic Watch Topic
  • New Topic