The role of the Package-info.java package 1. Provides convenience for labeling the annotation on the package 2. Declare the private class and constant 3 of the package. Provides the overall comment description code description for the package:
Package-Info.java File
@PackageAnotation (version= "1.0") PackageCom.ltao. Package. TestclassPackageInfo { Public voidCommon () {System.out.println ("SA"); }} classPackageinfogeneric<textendsThrowable> { PrivateT obj; Public voidset (T obj) { This. obj =obj; } Public voidCommon () {System.out.println (obj+ "SA"); }} InterfacePackageinfointeger { Public voidtest ();} classpackageconstants { Public Static FinalString Erroe_code = "100001";}
The Package-info file above defines classes, generics, interfaces, constants. These are package scope visible, this way can realize the package accessible scope class of uniform collation
Packageanotation.java defining annotations for cosmetic packages
@Target (elementtype.package) @Retention (retentionpolicy.runtime) @ Interface Packageanotation
{
Public String version () default "";
}
Packagecom.lt. Package. Test; Importjava.io.IOException; /*** Test the role of the Package-info.java file * 1, for labeling on the package annotation provide convenience;<br> * 2, declare the package's private classes and constants;<br> *@author[email protected] **/ Public classTestpackageinfo { Public Static voidMain (string[] args) {//1Package P = package.getpackage ("Com.lt.package.test"); if(P! =NULL&& p.isannotationpresent (packageanotation.class) {Packageanotation nav= P.getannotation (packageanotation.class); if(Nav! =NULL) {System.out.println ("Package version:" +nav.version ()); } } }}
Output: Package version:1.0 (Get the version information in the note)
When you use Javadoc to generate a doc document, Package-info.java also generates a corresponding document description, which can be described here in the face of the class.
The role of Packinfo-java