Packages don't provide security in any meaningful sense. However, they do help to support modularization via "package private" access:
package com.example;
public class Example {
int someMethod() { ... }
}
The access for someMethod
is package private, which means that it is only visible to other classes in the com.example
package. You can control the visibility of fields, classes and interfaces in the same way.
Note that this is NOT a credible security mechanism for most Java applications. It is simple for an application to use reflection to work around most (if not all) access restrictions based on access modifiers. The only way to stop that is to run untrusted code in a security sandbox that disables the use of the reflection APIs.