Package-info. JAVA Role and usage

Source: Internet
Author: User

Package-info.javafor programmers who often use external packages, they should be familiar with strangers. Because programmers who are not specialized in developing packages seldom need to pay attention to it, and often see him in other packages, the eyes are familiar. What are the characteristics and functions of it and how to use it? Programmers pay attention to mouth, why not create a try. OpenEclipse,new class,Oh, yes! Creation failed, '-' as special characters are not allowed to appear in the class name, what condition. Originally it was a special case, of course, to create, Notepad, good success. I like inquisitive, and what's special,GoogleYes , there are ancestors, turned around to collect. Features:first, it cannot be created casually. In theEclipsein whichPackage-infofiles cannot be created randomly and will be reported"Type name is Notvalid"error, the class name is invalid, the Java variable definition specification is: Letters, numbers, underscores, and the less commonly used $ symbol (incidentally,Javais to support the Chinese name of the variable, the habit of the challenge students can try to share this experience. Secondly, the object of service is very special. A class is a description of a category or set of things, such asDogThis class is a description of Wang Choi, thatPackage-infoWhat does this class describe? It always has to have a descriptive or stated object, which is a description and a record of the package information. Finally, the class cannot carry aPublic , Privateaccess rights. Package-info.javaagain how special, is also a class file, also will be compiled intoPackage-info.class, but inPackage-info.javaa class that can only declare the default access permission in the, that is, the friendly class. In fact, there are a few special places, such as can not inherit, no interface, no inter-class relationship (association, composition, aggregation, etc.).  The particularity of this document is finished, then say what it does, it has three functions:to label on the packageAnnotationprovide convenience;declare friendly classes and package constants;provides an overall comment description for the package. Let's build a project to demonstrate these three roles and build aPackage-infoof theJava Project , in the com.company Pack of three classes: Package-info.java is our key concern,Pkgannotation.java is a callout definition that is labeled on a package, andClient.java simulates the business Operation class. Its structure is as follows:

to label on the packageAnnotationProvide conveniencefirst define a package-type annotation, which can only be placed on one package:JavaCode        @Target (elementtype.package)@Retention (retentionpolicy.runtime)Public @interface Pkgannotation {    } then define aPackage-infoclass, this is a special class that looks at the code first:@PkgAnnotationPackage Com.company;very simple, just such a document, there is nothing inside, just these two words, noclassclass, there is no constant variable declaration. Then write a simulated trading class, the code is as followsJavaCode:   Public class Client {            Public static void Main (string[] args){  //Can be throughI/Ooperation or configuration item get package name           String pkgname = "Com.company"; Package pkg = package.getpackage (pkgname); //Get annotations on the package           annotation[] annotations = Pkg.getannotations (); //Iterate through the annotations array           For (Annotation an:annotations) {if (an instanceof pkgannotation) {System.out.println ("Hi,i ' m The Pkgannotation");                                         }              }          }      }  the results of the operation are as follows: hi,i ' m the pkgannotation! declaring friendly classes and package constants This is simple, and useful, such as a package with a lot of internal access to the class or constant, it can be unified into thePackage-infoclass, which is convenient and centrally managed, reducingFriendlyclasses wander around the situation, see Example: JavaCode: @PkgAnnotationPackage Com.company; //This is a package class that declares a common class used by a package, emphasizing that the package access rightsclass pkgclass{Public Void Test () {    }  }  //Kanetsune volume, only run in-package access, suitable for sub-"package" developmentclass pkgconst{static final String pacakge_const= "ABC"; }provides an overall comment description for the package if it is a "package" development, that is, a package to implement a business logic or function points, or modules, or components, you need a package has a good description of what the package is dry, what role, version changes, special instructions, and so on, as follows: JavaCode:        Package Com.company;  throughJavadocgenerated byAPIThe documentation is as follows:

This is placed under the packagepackage.htmIt's no different, it's justPackage-infoYou can better maintain the integrity of your document in your code, and you can synchronize your code with the document to update it .package.htmcan also be done, without arguing, the suggestion isJava 1.5The above versions are usedPackage-info.javato comment. with thePackage-infoRelated Issues In project development, common annotations that can be placed on a package are:Struts's @namespace, Hibernateof the@FilterDefand the@TypeDefand so on. Add these annotations to the package name in a class, under the package,Eclipsewill prompt"Package annotations must is in file Package-info.java", under which the package is establishedPackage-info.javafile, move the annotations here.  UseCheckstylea warning is reported when the plugin does a code check"Missing Package-info.java file."that's it .Package-infofile a curse, create one under each package.
get ""

Package-info. JAVA Role and usage

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.