• 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

StackOverFlow Exception help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this tiny piece of code .


public class Test
{
Test test = new Test();
String x="a";
public Test()
{
x = "b";
}
public static void main(String[] args)
{
System.out.println(new Test().x);
}
}



I really don't get it why this throws StackOverflowException.
There is someone who can explain how this code works ?

Please help me !
Thank you in advice .
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mazzoty Laurence wrote:I have this tiny little piece of code .


public class Test
{
Test test = new Test();
String x="a";
public Test()
{
x = "b";
}
public static void main(String[] args)
{
System.out.println(new Test().x);
}
}



I really don't get it why this throws StackOverflowException.



Basically, in the main() method, it instantiates a Test object. During this process, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated. This variable is for a Test object, which is assigned to the test variable. During the instantiation of that Test object, there is an instance variable which gets instantiated..... etc. etc. etc........ Stack overflow.

Henry
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Henry Wong!

(Initially, I thought that either you pasted the same text by mistake, or there's some serious glitch at coderanch server side )
 
mazzoty Laurence
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great answer , i spotted the way it works .


Thank you so much
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add, StackOverflowError is an Error(java.lang.StackOverflowError) and not an Exception...

However, I just re-checked the same as I was typing my post here and my Eclipse IDE threw up this interesting message

Exception in thread "main" java.lang.StackOverflowError
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)

Is the message here, misleading?
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain little more? I am still confuse about what is happening here.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a Test object is created in the main method, this Test object also instantiates another new Test object, which also instantiates another new Test object and so on.

It runs out of memory.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janki Shah wrote:Can any one explain little more? I am still confuse about what is happening here.



Sure...

1) The original post spoke about StackOverflowException which I corrected in my post as StackOverflowError since it is a Error and not a Exception.

2) However when I simulated the same by a test program, the message that I got at the console was "Exception in thread "main" - java.lang.StackOverflowError". Here I queried whether the output of console was misleading as it mentioned "Exception".

Hope this helps.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't it be HeapOverflowError since Objects and instance variables live on the heap ?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kondwani Chipeta wrote:Shouldn't it be HeapOverflowError since Objects and instance variables live on the heap ?


What actually happens is that the space allocated for keeping track of the stack of method calls runs out before the heap runs out. You create a Test object, which runs the Test constructor, which creates another, which runs the TestConstructor, etc.

You can potentially get the same error with any recursive method, regardless of whether it creates new objects or not. Try this:
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:
Exception in thread "main" java.lang.StackOverflowError
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)
at com.tools.java.praveen.misc.ArrayAndStringLengths.<init>(ArrayAndStringLengths.java:5)

Is the message here, misleading?


Yes, it's not entirely accurate. Maybe it's OK if you interpret it as "exception" (as in exceptional condition), rather than Exception.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:
What actually happens is that the space allocated for keeping track of the stack of method calls runs out before the heap runs out.



Yes, its actually like a race-condition...In case the heap runs out before the stack(lets say due to heavier objects), you get an OutOfMemoryError...

Matthew Brown wrote:Yes, it's not entirely accurate. Maybe it's OK if you interpret it as "exception" (as in exceptional condition), rather than Exception.



Sure, thanks for the clarification Matt
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic