aspose file tools*
The moose likes Java in General and the fly likes regarding StackOverflowError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "regarding StackOverflowError" Watch "regarding StackOverflowError" New topic
Author

regarding StackOverflowError

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
public class Test
{
Test t = new Test();
public static void main(String args[])
{
Test t1 = new Test();
t1.myMethod();
}
public void myMethod()
{
System.out.println("Hi");
}
}
If i run this code it is giving stack overflow error even if myMethod is accessed through t object.But if object t is made static it is working fine.I am not clear about this.please tell.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

"t" is an instance member of Test, meaning that every instance of Test has its own variable "t". So when you create an instance of Test, it has a variable "t", which is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test.While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test. While creating that new instance of Test, its variable "t" is initialized to hold a new instance of Test... and boom, you have a deep execution stack, because none of these calls to "new Test()" have returned yet. Each one invokes another one, until the stack overflows with call records.


[Jess in Action][AskingGoodQuestions]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Cute.

-Cameron McKenzie
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Thanks a lot for your reply...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: regarding StackOverflowError