• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why wouldn't a code using singleton pattern throw a StackOverflowException?  RSS feed

 
Edwardd Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading a chapter from a Java book which introduces singleton pattern. Basically such pattern looks like this:

I was wondering why wouldn't this code create a StackOveflowException, since this line is basically a recursive call to itself. Does this have something to do with hayInstance being final? Thanks in advance!
 
Vishal Ajwani
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there, I'm also a greenone here and I hope this answer helps you..
What we are doing here is making an object of the class HayStack(), which is static object, using a constructor, just one method is called. While in StackOverflowException, it is required that stack overflows due to recursive call of a method in the class. FYI, here although the constructor is made private, it can make objects inside the class. I hope you got it.
 
Vishal Ajwani
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the line

public static final HayStack hayStack = new HayStack();

Creates a new reference hayStack which then is assigned to the memory created by new HayStack(). So it's not a recursive call it is an initialization call.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable hayInstance is static so it is initialized only once.
You would get StackOverflowError if you removed the static keyword.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward,
The very first thing is stack overflow is the Error in java and the specific class associated with it is the StackOverflowError so their s not anything called  StackOverflowException in java.

regarding your query:
Their is not any recursive call here,here the object is created at the same time the variable is declared and final keyword wouldn't let you to change this reference,after this line,anywhere in your code.the static keyword is playing a role in sharing the same copy of variable for each instance of a class(though in your code you have only one instance but the main method cannot identify that single object till you will mark it static).
let me give you the example of recursive call:

here if you would see the getInstance(),it is recursing too deeply without any conditional end for the recursion.if you are lucky then you would get here a StackOverflowError.
actually their is a term "stack" in java.it is maintained in private by java virtual machine(JVM) during the time it runs.it holds the local variables and some results.their is another term called "frames".when ever you will invoke a method their will a frame gets created on the stack which also holds some data and partial results and return value of methods etc.stack has an optimization to expand and contract as er requirement but their is some maximum and minimum limit for this expansion and contraction.during a recursive call their are numerous frames gets created on stack and thus expansion takes place per creation of frame but a situation comes when the expansion reaches the maximum limit and after it throws a stack over flow error.

I hope things are pretty much clear now!!

Kind regards,
praveen.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Edwardd Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies! I understand that by assigning static to the variable hayInstance, the object referenced by hayInstance now belongs to the HayStorage class thus can be created only once. I still don't understand what actually happens after you call In my understanding, after this line is called, hayInstance is created, but the initialization of that new hayInstance object also includes another line of Is my understanding correct that this initialization line will just be ignored by JVM during the initialization process because there can be only one hayInstance object across the classes? Sorry if my expression is not very clear.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static members are initialized when the class is loaded (first time when the class is used).
This happens only once.
Static members are not initialized when an instance is created.

This has nothing to do with the fact that the class is supposed to be a singleton.
 
Edwardd Lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I can't believe I forgot that. That makes much more sense now. Thanks very much for the help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!