Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables in Threaded Static Methods

Status
Not open for further replies.

BillyKrow

Programmer
Aug 27, 2001
67
US
I have a static method I'm using as a utility in a Servrlet class. Since the servlet class will be threaded I'm worried about data integrity. The question is really simple. If I declare an array within a static method like this:

someObject [] arrayOfSomeOjbect = method ();

If method () returns an array of someObject am I actually allocating memory for a new array each time or do I risk the threat of the threads overwriting the variable?
I understand with new instances of objects I risk no threat but wasn't sure if this was the same case.
 
>> If method () returns an array of someObject am I
>> actually allocating memory for a new array each time or
>> do I risk the threat of the threads overwriting the
>> variable?

Strictly speaking the answer as you phrased the question is no. method returns a reference to an array object. So…
Code:
 someObject [] arrayOfSomeOjbect = method ();
arrayOfSomeObject catches the returned reference no allocation occurs at that time.

>> do I risk the threat of the threads overwriting the
>> variable?

That depends on what method() returns you. If "method" is returning a referece to a single instance of an array then yes. If on the other hand "method" is allocating a new array and returning the reference to that new array then the answer is no.


-pete
I just can't seem to get back my IntelliSense
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top