File local access


I have used nested classes which accessed the private members of top level classes for some time, and discovered that a top level class can access the private members of nested classes. Recently, I discovered that nested classes can access private member or other nested classes. i.e. where the two classes are not nested, but share a top level class.


Perhaps private should be called "file local" c.f. package local. ;)

public interface MyApp {
    class Runner {
        public static void main(String... args) {
            // access a private member of another class
            // in the same file, but not nested.
            SomeEnum.VALUE1.value = "Hello World"; 

    enum SomeEnum {
        private String value;

        SomeEnum(final String value) {
            this.value = value;
        public String toString() {
            return value;


  1. Eclipse will give you the following warning (if enabled):

    Write access to enclosing field MyApp.SomeEnum.value is emulated by a synthetic accessor method

    So it's actually using a method call.

  2. The JVM doesn't support access of private members (field, methods, constructors) from other classes, so the compiler generates accessor methods with names like access$100.

    AFAIK, The warning comes from the concern this could impact performance, or be confusing (as the generated method appears in the stack trace) You will get this warning even if an inner class accesses a private member of an other class.


Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues