Week 102 — What is the `package-info.java` file and what can be done with it?
Question of the Week #102
What is the
package-info.java file and what can be done with it?2 Replies
The
package-info.java can be used to provide Javadoc documentation for a package.
After creating a package-info.java file in some package, one would typically add a Javadoc comment followed by a package declaration stating the package the file is located in.
The javadoc tool will recognize this file and include the Javadoc comment in the generated documentation.This file can also be used to add annotations to a package.
For example, assume an annotation
com.example.annotations.MyAnnotation with the PACKAGE target:
This annotation can be used in a package-info.java file before the package declaration. Annotations in the package-info.java can be imported after the package declaration:
Annotations like these are considered to annotate the package.
An example of this is the @NullMarked annotation in jspecify that can be applied on packages and modules among other elements.
📖 Sample answer from dan1st