3

I am extending ArrayList to create a custom ArrayList that can be modified using normal ArrayList methods while iterating over it. For this I am also creating an Iterator.

public class SynchronizedList<E> extends ArrayList<E>
{
    // Fields here

    //Constructors and methods here

public class SynchronizedListIterator<E> implements Iterator<E>
{
    public int index;
    private E current;

    public boolean hasNext()
    {
        synchronized (/* reference to enclosing List object */) {
                    //code goes here
        }
        return false;
    }

    //more methods here
}
}

During my hasNext() and next() methods, I need to make sure the list is not modified (it can be modified at any other time). Hence I need to refer to my enclosing type in my synchronized() block.

user1299784
  • 2,019
  • 18
  • 30
  • possible duplicate of [Access Outer Class this instance](http://stackoverflow.com/questions/1721608/access-outer-class-this-instance) and [Getting hold of the outer class object from the inner class object](http://stackoverflow.com/questions/1816458/getting-hold-of-the-outer-class-object-from-the-inner-class-object) – Paul Bellora Apr 08 '12 at 02:57

1 Answers1

5

EnclosingType.this. So in your case, it would be SynchronizedList.this.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141