Originally posted by Khanh Nguyen:
Shamaila - I got two questions for clarification purposes. So if num was not a private variable then num would not be thread safe. Or if within doGet, num is set to a different value then num would not be threadsafe also. (
(first question) If num was not a private variable:
Actually static variable are not thread safe as all the instances of this class can access/modify them either by onjectName.staticVariableName or by ServletClassName.staticVariableName,
but as num is a private variable so you can not access it in either of these two ways.
But any method of the servlet class can modify its value. so all the instances of servlet class can modify the same static variable even if it is private. That's why private static variables can not be considered as thread safe.
(second question)if within doGet, num is set to a different value then num would not be threadsafe also:
In all the cases where we know that no method of the servlet class modify the value of a variable(static or instance) then we can say that this variable is theard safe. So thread safety of a variable depends upon the code. means depending on the imlementation of the servlet you can say that a particualr variable is thread safe even if the variable is an instance variable.