Packages (package) in Java

Source: Internet
Author: User

concept of the package

The concept of package in Java is similar to the concept of namespaces (namespace) in C + +, and you can restrict the scope of a class. The biggest difference is that the Baoyin in Java indicates the tree hierarchy of classes (and also the directory structure of Java source files). The advantage of this is that the uniqueness of the class can be limited by the requirement for file path uniqueness in the file system.

Code Organization

When writing a Java source code file (. java file), this file is often referred to as a compilation unit . There is a maximum of one public class allowed within the compilation unit, and the name of the class must be exactly the same as the file name (including case).

When compiling a . Java file, each class in the . Java file will have a . class output file with the same name as the class name. A Java runnable program is a set of . Class files that can be packaged and compressed into a Java document file (Jar package, a JAR document generator using Java). The Java interpreter is responsible for locating, loading, and interpreting these files.

A class library is actually a set of class files. Each of these . Java files allows a maximum of one public class, and any number of non-public classes. Therefore, each file has a component. If you want to organize these artifacts (each with a . Java file and several . class files) into different groups, you can use the keyword package in java.

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 a folder, the package also uses a tree-like 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.

Create a package

In Java, you use the package keyword to specify which packages (namespaces) your code belongs to.

Syntax format:

     Package pkg1[. pkg2[. Pkg3 ...];

Note the point:

(1) The name of the package implicitly points to the directory structure of the code.

(2) The public class name (and also the Java file name) under the same directory should be unique.

(3) Package declaration should be in the first line of the source file, each source file can have only one package declaration, each of the types in this file is applied to it.

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

(5) The package name is generally in lowercase letters.

Importing Packages

In Java, import the package by using the import keyword.

Syntax format:

import package1[.package2 ...]. (classname|*);

Cases:

Or take java.util.ArrayList for example. It is inconvenient to use it in a way that is a complete path to the class.

java.util.arraylist<string> list = new java.util.arraylist<string> ();

If you want to omit the previous path, you can use the import keyword.

import java.util.ArrayList;

After importing the package with import in the file, the code for the previous declaration list can be simplified as follows:

arraylist<string> list = new arraylist<string> ();

Summary

Why use a package?

1, convenient to the source files for classification maintenance;

2, solve the problem of class name conflict, under the same package can not exist two classes with the same name;

3. In a class, if you want to use a class under another package, you must import it by importing the fully qualified name of the import class;

4, fully qualified name, package name. class name; The fully qualified name of the class refers to the fully qualified name of the new Class (), and the generated object;

Packages (package) in 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.