Java Tour _ Object-oriented _ package

Source: Internet
Author: User

Http://www.runoob.com/java/java-package.html

The role of the package:

1. Organize similar or related classes or interfaces in the same package to facilitate the search and use of classes.

2. Like folders, packages are stored in a tree-like directory. The class names in the same package are different, and the class names in different packages can be the same.

When a class with the same name is called, the package name should be added, and the package can avoid name collisions.

3. The package also has access to a class that has access to the package to access it.

Java uses the package as a mechanism to prevent naming conflicts, access control, and to provide search and location classes, interfaces, enumerations, and annotations.

 Package Net.java.util;  Public class something{...    }

Its path is saved by Net/java/util/something.java.

The following are the packages in Java:

Java.lang--The class of packaging basics

Java.io--functions with input and output functions

To create a package:

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 (unamed).

Import Keyword:

In order to be able to use the members of a package, we need to explicitly import the package in a Java program.

In the Java source file, the import statement should be located after the package statement, and before all classes are defined, there can be no or more than one.

There are several ways to implement classes that are introduced into other packages:

1. Otherpackage.otherclass in the Code

2.import otherpackage.*;

3.import Otherpackage.otherclass;

Note: The class file can contain any number of import declarations. The import declaration must precede the class declaration after the package Declaration.

Java Tour _ Object-oriented _ package

Related Article

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.