The role of Packinfo-java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.