Access to private parts of a package in Java

Interaction with Android API sources. There is a FileDescriptor with a descriptor data element without an access modifier:

 int descriptor; 

Then there is the FileOutputStream class that creates a new FileDescriptor and assigns to this field:

 fd = new FileDescriptor(); fd.descriptor = fileSystem.open(...); 

How is this compatible with the Java field access control model? I thought that the private fields of the package are not accessible from outside the declaring class, and there is no concept of friendship, as in C ++.

+4
source share
3 answers

In principle, private packages can be accessed at the class and package levels:

From source :

 Access Levels Modifier Class Package Subclass World public YYYY protected YYYN no modifier YYNN private YNNN 
+19
source

An ad without a modifier, for example

int descriptor;

Are the packages that are most often called DEFAULT available, available in the package, and not outside the package. Any class inside one package can access them, but they are not visible outside the package.

See here for more details.

 Access Levels Modifier Class Package Subclass World public YYYY protected YYYN no modifier YYNN private YNNN 
+1
source

A batch private field (and everything else) is simple: private in its package. This means that no other class can access a field (or other object) outside the scope of the package. See here for more details.

0
source

Source: https://habr.com/ru/post/1439413/


All Articles