Package-private members in java.util.ArrayList

Zheka Kozlov orionllmain at gmail.com
Mon May 10 05:45:14 UTC 2021


Hi!

JEP 181 (Nestmates) was implemented in Java 11, however, there are still
many library classes with members that were declared package-private to
avoid synthetic bridge methods. For example, ArrayList and its friends have
the following declarations:

• transient Object[] elementData; // non-private to simplify nested class
access
• private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        // prevent creating a synthetic constructor
        Itr() {}
        ...

        final void checkForComodification() {
            ...
        }
  }
• private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            super();
            cursor = index;
        }
        ...
   }
• final class ArrayListSpliterator implements Spliterator<E> {
        ArrayListSpliterator(int origin, int fence, int expectedModCount) {
            this.index = origin;
            this.fence = fence;
            this.expectedModCount = expectedModCount;
        }
        ...
   }

As far as I understand, this is no longer relevant and we can freely make
all these members private without undermining performance. This will make
the code more readable and will stop confusing readers. Can we do it?

Zheka.


More information about the core-libs-dev mailing list