26

In the java.util.ArrayList class, the object array for the list's elements is defined as package-private:

transient Object[] elementData; // non-private to simplify nested class access

The comment states that the reason why this field is not private is easier access in nested classes. However, nested classes can access private data of the enclosing class just fine. So why is elementData not private? Is there something happening in the background (e.g., at compilation time)?

3 Answers3

23

When you access a private field from a nested class, the compiler actually generates a synthetic accessor method that is package-visible, and then uses that for the access. It can't access the private member directly, so to avoid that indirection you can make the member package-visible instead.

Here's an answer with more details.

Pezo
  • 1,458
  • 9
  • 15
12

That comment is outdated. With the introduction of this JEP, there will be no syntactic method created by the compiler anymore; and that was introduced in jdk-11.

Before that change, the problem for such a highly used structure like ArrayList, was that another method in the call-stack (for accessing that private field) could potentially have a high cost in critical paths. To get aways from calling one more method, you could declare the field without private.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • The code of `ArrayList` and other classes doesn't seem to be updated to take advantage of the nest-mate functionality. [This mail](https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-May/077275.html) to a JDK mailing list suggests making such updates. – Lii May 12 '21 at 06:57
-1

However, nested classes can access private data of the enclosing class just fine

This is not true for nested classes that are static.

And at least the class ArrayList.SubList is static and accesses elementData.

also see here: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

What I claimed here is plain wrong as @tevemadar made clear to me. This is a working example that @tevemadar provided:

public class Test
{
    private int a = 0;

    public static void main(String[] args)
    {
        new Inner().doTest(new Test());
    }

    static class Inner
    {
        void doTest(Test t)
        {
            System.out.println(t.a);
        }
    }
}
uli
  • 31
  • 4
  • That's a constraint/feature of `static` what you probably mean. A static inner class can't access fields of the outer object directly because there is no outer object (as for `static` things in general). That's why the article writes about the need of an object reference. – tevemadar Apr 29 '21 at 08:28
  • Yes, that is a feature of static. But ArrayList.Sublist can't access dataElement, since it is a static class. So it acccesses it via a reference of ArrrayList, called root. And that does only work if elementData is package private. And the original question was, why that field is package protected and not private. – uli Apr 29 '21 at 08:38
  • 1
    *And that does only work if elementData is package private.* - Nope, it would work with an explicit `private` too. Try this: `public class Test {private int a=0;public static void main(String[] args){new Inner().doTest(new Test());}static class Inner{void doTest(Test t){System.out.println(t.a);}};}` – tevemadar Apr 29 '21 at 08:49
  • You are right. Thanks for beeing so tenacious. That helped me to get rid of my false assumption. – uli Apr 29 '21 at 09:17