• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private varibles in servlet

 
sitaram irrinki
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we write private variables in servlet?if yes how they are managed by multiple threads?
 
Makarand Parab
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
If it is a private instance variable, that variable will be shared by all the threads. Having a instance variable in servlet is highly discouraged. Well i never define one.
If you define a variable inside the service, dopost, doget methods as it is a local variable for the method, it gets created per thread and is blown off when the method ends.

Hope it makes sense
Let me know if i am wrong.

Regards
Makarand Parab
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Makarand is correct. Object vars are not thread safe in a servlet. static final is OK and possibly even variables that are ONLY initialized in the init method and therefore not changed. If this is a concern make them member/method vars where they will be thread safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic