• 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

regarding StackOverflowError

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cute.

-Cameron McKenzie
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your reply...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic