Search code examples
javadefaultdesign-decisionsdesign-consideration

In Java, why are class members accessible to members of the same package by default?


I understand that unlike in C++, if I don't specify "public" or "private" when declaring a data member, it can be accessed from anywhere in the same package.

The designers of the Java language could have chosen the opposite, but instead they preferred to make class members public (in the same package) by default.

Any idea why?


Solution

  • They're not public, they're accessible to members of the same package. The Java ethos is that a given package represents a coherent set of coperating responsibilities, so by default they should be able to interoperate.

    You can always use a specific access level if you wish to enforce differnt semantics.