Hi all,
Following is what I'm trying to do.
1) I hava a baseclass CLass A which has a instance
String variable.
Abstract class A
{
protected String var = "base";
abstract protected void modify(string input);
}
2) I have a subclass Class B which extends Class A
class B extends Class A
{
modify(String input){
if (input.equlasIgnorecase("A")
var = "subclass";
else
var ="SubClass1"
}
}
My question is Class A is accessed by multiple threads so ineffect multiple threads are modifiying the the variable "Var" based on the input value. Do I need to synchronize on the "Var" variable to make sure right vaues are assigned? IF so can is below version the right way to do it? Please bear with me as I'm new to
java . Also will it take any performance hit?
class B extends Class A
{
modify(String input){
synchronized (var){
if (input.equlasIgnorecase("A")
var = "subclass";
else
var ="SubClass1"
}
}
}
THanks