• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding StackOverflowError  RSS feed

 
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
Sheriff
Posts: 24217
38
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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!