Private access is more restrictive than protected : protected attributes and methods are still available by simply subclassing the class. Protected members are (or may be) intended to be inherited, while members with a closed package are not.
Frequent package members are often used, so multilpe classes within a package can access implementation-specific attributes or (utility) methods.
Good examples of this are the private-package String constructor and the StringBuilder.value char array:
String(char[] value, boolean share) {
Thus, classes inside the java.lang can efficiently create new Strings if the content is already present in char[] without compromising security. You cannot do this from your application, because if you could, you would have access (reference) to the char String internal array, which is immutable (reflection does not count!).
In StringBuilder (or rather AbstractStringBuilder , where the implementation is implemented) the char array containing the current char[] value , and the access method for this char[] getValue() also batch, so various useful String methods like contentEquals(StringBuffer sb) and contentEquals(CharSequence cs) can use this for efficiency and faster comparisons without exposing the inner char array to the world.
icza Feb 10 '15 at 7:05 2015-02-10 07:05
source share