user2434 user2434 - 1 year ago 115
Java Question

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 ?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download