Number of class files in java package constant

I am new to java.My friend asked me this question today. And I'm looking for an answer to it. How to make the number of class files in a package permanent? that is, even if you can access this package, they should not add a new class to an existing package.

+4
source share
2 answers

You want to seal the packages. After sealing, all classes from the package should come from one JAR file. This basically boils down to adding a package to the manifest:

Name: myCompany/myPackage/ Sealed: true 
+5
source

This is called package compaction and works at the jar file level.

From the official trail:

Packages in JAR files may optionally be sealed, which means that all classes defined in this package must be archived in the same JAR file . For example, you can seal a package to ensure version consistency between classes in your software.

To clarify: since classes must come from the same jar file, no one can add classes to your package since new classes will not come from your jar file.

+3
source

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


All Articles