In java learning, the use of package packages, the import key words (small records in java learning), and packageimport

Source: Internet
Author: User

In java learning, the use of package packages, the import key words (small records in java learning), and packageimport

Learning java,PackagePackage usage and import of the import key words (small record in java learning)Author: Star)

 

Use of packages

Package: You can put the compiled class file into the specified package.

Java package: equivalent to a folder on a computer.

Definition package: package keyword definition package

Create a package file during compilation:

ConsoleCmdInput: javac-d. Class Name. java // create a folder specified by the package. Created in the current path.

(Accessed)

(Accessing the Dod class)

Note:

1. classes must be public-modified and public.

2. The accessed class must be compiled first.

3. To use other classes in other classes, use the full name of the class. (This method is too troublesome, so we have a better method to export packages)

Keyword of the import package: import

The preceding code can be optimized to import dog. Dog to the header file.

Then, the following Dog class can be directly created without writing the package name: Dog dog = new Dog ();

Export package format:

1. import package name 1. Package name 2. Package name 3... class name;

2. import package name. *; import all directly. We recommend that you use the first package import method.

Benefits of using packages:

1. It can prevent conflicts of class files with the same name.

2. Separate the source file from the compiled file.

Precautions for using the package:

1. Only one package can be defined in the Java class

(This is wrong)

2. The package must be the first statement to describe the class.

 

Knowledge Point exercises:

You can define two classes in two different packages, and then call each other for the two classes ~

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.