This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variables in servlets  RSS feed

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the static variables in servlet thread-safe?
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the answer. Thanks!!

Answer was there in another post.
 
Dave Tolls
Ranch Hand
Posts: 2830
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the answer you found was?
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All variables and attributes except request attributes and local variables can be manipulated.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that answer mean that static variables are thread safe or not?


 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not thread safe. Am I correct?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds right to me. Just wanted to make sure this thread was left in a good state.
There is nothing more frustrating for people than to find a post with their exact problem and then see "oh I got the answer" with no more details than that

In fact, servlets are irrelevant here. Static variables are NEVER thread safe.
static means it is a class variable - it is associated with the class definition rather than any given instance.
All instances of that class share that same attribute. Multiple threads can easily interfere with one another.

I steer clear of static except for constants, because they make JUnit testing extremely troublesome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!