-1

Possible Duplicate:
Synchronization of non-final field

I have an arrayList and when i use this:

//declare it as a global ArrayList
private ArrayList <Human> myList = new ArrayList <Human>() ;

//inside a method
synchronized (myList){     
    for(  ListIterator<Human> it = myList.listIterator();it.hasNext();){  
       Human = it.next();

it tells me Synchronization on non-final field. what should i do?

Community
  • 1
  • 1
arnold leki
  • 267
  • 2
  • 9
  • 18

1 Answers1

2

You can synchronize on non-final field (myList on this example)

You can use synchronized block without parameter

synchronized{     
    for(  ListIterator<Human> it = myList.listIterator();it.hasNext();){  
       Human = it.next();

Or create another class property with "final" attribute.

PS. You can use foreach loop as well

for(Human h : myList){  
           h.getName();
}
darek
  • 301
  • 1
  • 8