Java Packages (Package)

Source: Internet
Author: User

To better organize classes, Java provides a package mechanism for distinguishing the namespace of a class name.

The role of the package
    • 1, the function of similar or related classes or interfaces organized in the same package, convenient for the search and use of the class.

    • 2, like the folder, the package also uses the tree directory storage. The class names in the same package are different, and the names of the classes in different packages can be the same, and the package name should be distinguished when calling two classes of the same class name in different packages. Therefore, packages can avoid name collisions.

    • 3. The package also restricts access, and classes that have access to the package can access the classes in a package.

Java uses the package as a mechanism to prevent naming conflicts, access control, to provide search and locate classes (class), interfaces, enumerations (enumerations), and annotations (annotation), and so on.

The syntax format for a package statement is:

Package pkg1[. pkg2[. pkg3... ]];

For example, a Something.java file it's content

Package net. Java. Util; public class Something{ ... }

Then its path should be saved by Net/java/util/something.java . Package is the role of different Java programs to save, more convenient to be called by other Java programs.

A package can be defined as a set of interconnected types (classes, interfaces, enumerations, and annotations) that provide access protection and namespace management capabilities for these types.

Here are some of the packages in Java:

    • Java.lang-Class for Package basics
    • java.io-functions that contain input and output functions

Developers can package their own set of classes and interfaces, and define their own packages. And in real-world development, it's worth advocating that when you do your own implementation of classes, grouping related classes makes it easier for other programmers to determine which classes, interfaces, enumerations, and annotations are relevant.

Because the package creates a new namespace (namespace), it does not create a naming conflict with any names in other packages. With this mechanism, it is easier to implement access control and to make locating related classes easier.

Create a Package

When creating a package, you need to give the package a proper name. After that, if the other source file contains the class, interface, enumeration, or annotation type provided by the package, the package's declaration must be placed at the beginning of the source file.

The package declaration should be in the first line of the source file, and each source file can have only one package declaration, and each of the types in the file is applied to it.

If a package declaration is not used in a source file, then the classes, functions, enumerations, comments, and so on will be placed in a nameless package (unnamed).

Java Packages (Package)

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.