Java Packages (package) and Import keywords

Source: Internet
Author: User



One, the Java use of packages (package) This mechanism is to prevent naming conflicts, access control

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.

The reputation method is as follows:

For example, 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) For example, a Something.java file with its contents


Package Xx.bb.aa
public class something{
...
}


Note: All classes in the Something.java compilation unit are placed in the XX.BB.AA package, and the packet's path should be saved Net/java/something.java. Package is the role of different Java programs to save, more convenient to be called by other Java programs.

In addition, the files stored in the package are generally divided into three types

1,java program source file with. java extension. Java file is a standalone compilation unit, similar to the CPP file in C + +, But it does not require. h files, as long as. Java is enough, a. java file can contain a public class, several package classes (the Package class feature does not have any access control adornments), and implicit classes, You can also include several classes of protected and private. Each class will generate a separate. class file at compile time, so. Java and. Class are not one-to-many relationships, but. java and public classes are one-to-a-kind. All of these. Class are determined by the package statement at the beginning of this. Java to determine its location in the package.

2, compiled Java class file with the extension. class.

3, other files, any other files, also known as Resource sample files, XML files, MP3 files, AVI files, text files ...


Second, import keywords : Just a kind of let you steal a little lazy method of typing, will not affect your classpath, this point you have to remember, there is no non-use import reasons, with the import will not be similar to C embedded in a file content effect, It's just a convenient way to do it. Not in the Classpath class, let you import also useless.

If you do not import, you use ArrayList this class, you need to write

Java.util.ArrayList.

and using the import java.util.ArrayList;

Later in the code to write ArrayList on it, easy. Import can use the wildcard character *,* to represent all classes under a package, excluding subdirectories.

Import java.awt.*

Not equal to

Import java.awt.*

Import java.awt.event.*

If you want to abbreviated java.awt.event and java.awt under the class, you can not lazy, two are to import.

(for example in the package, to reference it in another class requires import xx.bb.aa.Something)
)




Java Packages (package) and Import keywords

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.