• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton in a servlet  RSS feed

 
Luke Kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use a class which implements singleton in my servlet.
Here is what i was tring to do:



I call the above class from doGet like this:



and when I refresh it, I always get the same number.
If i understood correcty each request makes a new thread to run, which calls service()->doPost/Get. There is always 1 instance of a servlet.
So if the above statemen is correct shouldn`t it always generate a new instance of Start class(in a new thread), so that i would benefit from singleton only if I would like to instantiate more then 1 Start class in the same thread/request. Does it have something to do with garbage collector?

Thanks in advance.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No; Start is a singleton--there's only one. That's what a singleton is. By what mechanism would a new Start be created?
 
Luke Kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:No; Start is a singleton--there's only one. That's what a singleton is. By what mechanism would a new Start be created?


It is created when a request is made.



Start init = Start.getStart(); // gets the instance
Doesn`t the above statemant load a class and then initiate a fresh object of type Start whenever it is first called from a thread/request?. and if I would make a new object like this "Start init2 = Start.getStart();"(in the same request/thread) i would get the same reference as init.
Shouldnt I get with each new request a new Start object, and in the same request i shouldn`t be able to make the same object twice?
Or this is not how it works?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. "Start.getStart()" loads the Start class only once, the first time you refer to it. That's what it means to be a singleton. Remember the JVM doesn't go away after your request is completed.

If you want a different instance every time you call the getStart() method then just don't use a Singleton. Just create a new Start object.
 
Luke Kamble
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No. "Start.getStart()" loads the Start class only once, the first time you refer to it. That's what it means to be a singleton. Remember the JVM doesn't go away after your request is completed.

If you want a different instance every time you call the getStart() method then just don't use a Singleton. Just create a new Start object.


Thanks alot, now i get it! :)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, by the way!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!