简体   繁体   中英

Java package-private access modifier behavior in case of package collision

Let's say, I am using a 3rd party jar A.jar in my project, with some members (classes, methods etc.) with default access (package private). Now, if I create the same package name in my project, I am able to access the members with default access modifier from A.jar (I tried this with Java Projects in Eclipse).

Oracle's document says:

If a class has no modifier (the default, also known as package-private), it is visible only within its own package

Isn't this philosophy violated in this particular use case(that too without using reflection), given that it is not what the developer of A.jar would have intended, as she would have wanted only classes in her package to access this particular member?

Also does it mean, that package-private is a convenience mechanism, but can't be used for securing the data etc.?

Isn't this philosophy violated in this particular use case(that too without using reflection), given that it is not what the developer of A.jar would have intended, as she would have wanted only classes in her package to access this particular member?

If she used default access modifier she would have less interested in securing her default members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM