• 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

weird problem

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the code below i'm getting following runtime error error

package project;

class B {

int a;
B d = new B();
}

class C {
public static void main(String args[])
{

C c = new C();
B b = new B();
System.out.println(b.d.a);

}

}



can anybody explain why???
--------------------------------------------------
compile:
run:
Exception in thread "main" java.lang.StackOverflowError
at project.B.<init>(C.java:6)
[.. removed 200+ repeated lines]
at project.B.<init>(C.java:6)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)



[ October 19, 2007: Message edited by: Eric Pascarello ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rahul mehra:
for the code below i'm getting following runtime error error

package project;

class B {

int a;
B d = new B();
}

class C {
public static void main(String args[])
{

C c = new C();
B b = new B();
System.out.println(b.d.a);

}

}

can anybody explain why???
--------------------------------------------------
compile:
run:
Exception in thread "main" java.lang.StackOverflowError
at project.B.<init>(C.java:6)
at project.B.<init>(C.java:6)
at project.B.<init>(C.java:6)
...
at project.B.<init>(C.java:6)
at project.B.<init>(C.java:6)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)



In class B, you have a field of type B, with an immediate initialization. So when you create an instance of B, this field will be set.

However, this field is also of type B. So this is what will happen:
B in main initializing
- B in B in main initializing
- B in B in B in main initializing

etc.

After a while the stack space runs out and you get this error.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have exactly 1024 repeats of
at project.B.<init>(C.java:6)

in your posting.

Buy a better computer, maybe you get 2048.


Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic