dcsimg
How static declaration affects methods.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, August 31, 2005 05:31 AM

Hi I am using a class say class Address { public String strName; public String strAddress; public String indicator; public static compareAddress(Address add1, Address add2) { if(add1.strName.equals(add2.strName)) { if(add1.strAddress.equals(add2.strAddress)) { add1.indicator = true; } else { add1.indicator = false; } } } } Now I am calling Address.compareAddress(add1,add2) from outside. If there are multiple threads calling this static method, will it work right? Is it going to be thread safe? Regards Vis   More>>

Hi

I am using a class say

			
class Address
{
public String strName;
public String strAddress;
public String indicator;


public static compareAddress(Address add1, Address add2)
{
if(add1.strName.equals(add2.strName))
{
if(add1.strAddress.equals(add2.strAddress))
{
add1.indicator = true;
}
else
{
add1.indicator = false;
}
}
}


}




Now I am calling Address.compareAddress(add1,add2) from outside. If there are multiple threads calling this static
method, will it work right? Is it going to be thread safe?



Regards
Vishal    <<Less

Re: How static declaration affects methods.

Posted By:   Jani_Kaarela  
Posted On:   Thursday, September 1, 2005 02:53 AM


I seriously think you should stick to learning the basics first and then worry about thread safety. Learn about types, methods, return values etc. Why should you worry about threads when your code doesn't even compile?



Find some nice tutorials to help you get started. Here are a few resources:


Also, use an IDE, it greatly enhances productivity and points out the most obvious errors (syntax etc.). I recommend Eclipse, which has a good feature set and is free.



But back to the original question... No, it's not thread safe, because the instance variables are public - any other thread could modify an Address while another is calling compareAddress(). You should consider a) making Address immutable or b) synchronize variable accessor methods. But as I said: start with the basics!

Re: How static declaration affects methods.

Posted By:   Almagest_FUTT  
Posted On:   Wednesday, August 31, 2005 06:32 AM

Yes (assuming i would compile).
But you definitely should take some time thinking your question and thread-safety through.
About | Sitemap | Contact