Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is singleton stateful?

Hi I got this asked in an interview question paper.

Singleton and Prototype (non-singleton) which is stateful and which is stateless

I am not sure if prototype is stateless ? Is there any problem with the question ?

like image 240
user2434 Avatar asked Dec 07 '22 19:12

user2434


1 Answers

The question itself is poorly worded. You can have state in both Singletons and Prototypes (instances), as in it is legal code, but you do not need to have state in either cases. Since Spring is mentioned, I will try to answer this in regards to working with Spring.

In terms of Spring bean scope, singleton will cause the ApplicationContext to create a single instance and use that instance everywhere the bean is asked for. prototype will cause the ApplicationContext to create a new instance each time the bean is asked for.

It is ok for both of these to be stateful.

like image 129
nicholas.hauschild Avatar answered Dec 09 '22 15:12

nicholas.hauschild