List as Class Variable and Local Variable
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, December 30, 2003 06:32 AM

I am running out into this silly problem and I am finding tricky to find what is going wrong!It may have have to do woth Class/Local variable issue also but nor so sure!! I have a List which is a public class variable . I store retrevied values into it in a method using 'this' keyword so that I can access retreived list anywhere in my class. public class aClass{ public List retreivedList ; public List syncretreivedList ; private void amethod(String s){ List alist = null; this.retreivedList = someobject.getRetrevedValues(); //Then perform follwoing things: alist = this.retreivedList ; debug("BEFORE ::SIZE = alist.size()= " + alist.size()    More>>


I am running out into this silly problem and I am finding tricky to find what is going wrong!It may have have to do woth Class/Local variable issue also but nor so sure!!
I have a List which is a public class variable .
I store retrevied values into it in a method using 'this'
keyword so that I can access retreived list anywhere in my class.

			
public class aClass{
public List retreivedList ;
public List syncretreivedList ;

private void amethod(String s){
List alist = null;
this.retreivedList = someobject.getRetrevedValues();
//Then perform follwoing things:
alist = this.retreivedList ;
debug("BEFORE ::SIZE = alist.size()= " + alist.size() );
this.syncretreivedList = Collections.synchronizedList(alist);
/*retreve a portion of buffer size where iBufferSize has some int value*/
this.syncretreivedList.subList(iBufferSize, this.syncretreivedList.size()).clear();
debug("AFTER manipulation :SIZE = alist.size() = " + alist.size() + " and this.retreivedList.size() = " + this.retreivedList.size());



And it prints same sublit size values for both alist.size() as well as this.retreivedList.size() while I want this.retreivedList.size() to print the original value [not after sublist] size.

I have not defined retreivedList as local in my method.

Any help?

Please use html tags to format code blocks.

   <<Less

Re: List as Class Variable and Local Variable

Posted By:   Anin_Mathen  
Posted On:   Friday, January 2, 2004 11:55 AM

Vivek,


Your question is kinda missing in some details but from what I could surmise, this is the answer I have for you.


Just by synchronizing your list doesnt make it a different copy. All the assignment that you are performing in your actions in the method are nothing but reference assignments. So when you perform a subList() call you are actually affecting both the syncretrievedList as well as the retrievedList.


The solution is to use shallow or deep cloning to get a copy of the original object.


That should explain why you are getting the same sizes.


Hope this helps.

Anin
About | Sitemap | Contact