Reputation: 1396
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)?
Upvotes: 27
Views: 979
Reputation: 120968
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
.
Upvotes: 13
Reputation: 31
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);
}
}
}
Upvotes: -1
Reputation: 1448
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.
Upvotes: 23