• 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

StackOverflowError

 
Greenhorn
Posts: 27
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this won't compile , it throws null pointer exception , it right but while i try to inialize the m1

it throws Java.lang.StackOverflowError why it throws StackOverFlowError ?

Thanks it advance
kumaresan N
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

look at "LINE 1".
You wrongly declared a instance variable in the class.
But with the code fixed this way, it will crash in "LINE 2", as m1.m1 is null, because it is initialized with the default constructor.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------------------------------------------------------------
while i try to inialize the m1

code:


class Mixer {
Mixer m1 = new Mixer();
Mixer() {
}
Mixer(Mixer m) {
m1 = m;
}
public static void main(String[] args) {
Mixer m2 = new Mixer();
Mixer m3 = new Mixer(m2);
m3.go();
Mixer m4 = m3.m1;
m4.go();
Mixer m5 = m2.m1;
m5.go();
}
void go() {
System.out.print("hi ");
}
}




it throws Java.lang.StackOverflowError why it throws StackOverFlowError ?
-----------------------------------------------------------------------------

When the program reaches line Mixer m2 = new Mixer(); an Mixer objext will be created. While creating that object the declaration Mixer m1 = new Mixer(); creates another object. So this looping happens continously which breaks 'stack'
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tommaso Nuccio:
Hi

look at "LINE 1".
You wrongly declared a instance variable in the class.
But with the code fixed this way, it will crash in "LINE 2", as m1.m1 is null, because it is initialized with the default constructor.



Just as a sidenote.

As the constructor has been provided explicitly it is not the default constrcutor. Secondly the access specifier also doesn't matches that with that of the class (in case of a default constructor it does). So a no-arg constructor would be a better way to refer to it.
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:


Just as a sidenote.

As the constructor has been provided explicitly it is not the default constrcutor. Secondly the access specifier also doesn't matches that with that of the class (in case of a default constructor it does). So a no-arg constructor would be a better way to refer to it.



That's what I meant by "default constructor".
 
reply
    Bookmark Topic Watch Topic
  • New Topic