How do I access the Enclosing class from the Inner class?

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

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 that the list is not changed (it can be changed at any other time). So I need to reference my private type in my synchronized () block.

+3


source to share


1 answer


EnclosingType.this

... So in your case it will be SynchronizedList.this

.



+5


source







All Articles