• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack Over Flow - Why?  RSS feed

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code in my class--

public class OutPut
{


static other OBJ = new other();



Now the program I wrote works. But if I type this :

public class OutPut
{


other OBJ = new other();


I get a stack overflow. Why do I need to mark it as static? I just
want to understand why it needs to be done that way.
In my class we are working on extending classes, passing variables
from method-to-method etc so this is all just for
training.

My program works I am just not clear on the Theory.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olivier,
Welcome to JavaRanch!

Does "other" extend "OutPut"? If so, you have infinite recursion. Instanatiating "other" goes to the superclass to instantiate "other" which goes to the superclass to instantiate "other" ....

Note that Java class names traditionally being with a capital by convention. Which would give you "Other".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking a look at the stacktrace typically gives a good clue at what happens in cases of stack overflows.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!