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 ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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,
singletonwill cause theApplicationContextto create a single instance and use that instance everywhere the bean is asked for.prototypewill cause theApplicationContextto create a new instance each time the bean is asked for.It is ok for both of these to be stateful.