• 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

inner, outer disturbance?????

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just look this code:
class outer1{
outer1 ou1 = new outer1();
outer1.inner1 in1 = ou1.new inner1();
class inner1{
int x = 100;
}
}
class outer2{
class inner2 extends outer1{
inner2(){
System.out.println(in1.x);
}
}
public static void main(String args[]){
outer2 ou2 = new outer2();
outer2.inner2 in2 = ou2.new inner2();
}
}
what is wrong with this code because it compiles fine but generate runtime error about StackFullError (something).
Please tell me how can i correct it and why stack becomes full.
thanx.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in the blue code:
Originally posted by Farhan Tariq:
<pre>class outer1 { outer1 ou1 = new outer1(); outer1.inner1 in1 = ou1.new inner1(); class inner1{ int x = 100; } }</pre>

This is recursive code: when you create an instance of outer1, you also create an instance variable ou1 that references a new instance of outer1 which in turn has its own instance variable ou1 that references a new instance of outer1 and so on until your stack blows up.
Remove "new outer1()" and you should be fine.
If you need an outer1 to reference another outer1, do it in either a regular instance method or by assigning a parameter value passed to a constructor. Don't create a new outer1 instance in any code that is executed during instantiation which includes constructors and instance variable initialization.
Junilu
(rephrased)

[This message has been edited by JUNILU LACAR (edited May 25, 2001).]
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Junilu!!
My problem is cleared if i declare "outer1 ou1 = new outer1();" as static. You debug my program fine. Thanx for reasoning.
Farhan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic