Java: Why can I access private fields of other objects?

As you may have discovered, this compiles:

class MyClass {

    private int i = 7;

    public void m(MyClass notThis) {
        System.err.println(notThis.i);
    }
}

The reason is that access modifiers work on class level and not on object level. Two different objects of the same class can access each others private members.

That’s strange, why doesn’t it work on object level?

This is a good question. Smalltalk, for instance, does in fact enforce access control on object level.

Here are three reasons:

Efficiency

Enforcing it on class level allows the compiler to catch all violations in compiletime. Had it been on object level on the other hand, the compiler would have had to do one of the following things:

Locality

The field you’re accessing (even if it’s of a different object in runtime) is declared in the file you’re editing. If you mess something up by modifying another objects state, the fault is in the file you have in front of you. There’s no spooky action at a distance.

Convenience

Some methods such as equals, clone and copy constructors would be tricky to write without giving up encapsulation.

Comments

Be the first to comment!