Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StackOverFlowError

 
Sandaru Jayathilake
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this give an StackOverFlowError in the line marked? When I make variable "a" static, it doesn't.

 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandaru Jayathilake wrote:Why does this give an StackOverFlowError in the line marked? When I make variable "a" static, it doesn't.


Why would you need "a" BankAccount inside a BankAccount instance ?
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandaru,
Your current code will keep on creating BankAccount Objects inside BankAccount objects till the Stack is full. That is why the error.
 
Thyagarajan Ramanujam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not due to multiple object creation in the for loop.

When you have a non-static object of a class as instance variable within the class itself, you will get this exception.

when you say new BankAccount(), BankAccount object is created which itself creates another BankAccount object and this goes on happenning and eventually you will end up with StackOverFlowError.

but when you declare the object as static, you have only one object. so you dont get that exception.
 
Sandaru Jayathilake
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!

What I wanted was just an application of static modifier!
To test it I tried creating instances here and there.
Now I understand what is wrong.

Thanks again!
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandaru Jayathilake wrote:Thanks all!

What I wanted was just an application of static modifier!
To test it I tried creating instances here and there.
Now I understand what is wrong.

Thanks again!


Welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic