Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding StackOverflowError

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cute.

-Cameron McKenzie
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic