Use of Java Packages

Source: Internet
Author: User

A package is a mechanism that distinguishes a class namespace from the Java language, which is a kind of file organization and management method of a class, a set of classes or interfaces that are functionally similar or related. The Java package provides a management mechanism for access and naming, which is a very basic but very important concept in Java.

The role of a package

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

2 As with folders, packages are stored in a tree-like directory. 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 packages also restrict access, and classes that have access to the package can access classes in a package.

Second, the definition of the package

In a. java file, you can have a public class and multiple non-public classes, and if you want to organize these classes in a package, the first line in the. java file, except for the comments, is implemented using the keyword packages. When you need to invoke a class in this package, you can import it using the keyword import. When defining a package, you should be aware of several points:

1 in order to keep the package name as unique as possible, the package name is usually defined in lowercase and in the form of an inverted-connected URL. For example: Com.hank.www indicates that the file path for package file placement is com/hank/www.

2 When naming packages, you should avoid using names that conflict with the system, such as Java.lang, java.swing, and so on.

Iii. Java package creation and use steps

Step 1 Create the Java folder under the C packing directory, under which the source files Helloworld.java and Mytest.java are placed. Write the package source file code Helloworld.java and the test feed file code Mytest.java. As follows:

Helloworld.java

Package com.hank.www;

public class HelloWorld

{

Public HelloWorld () {System.out.println ("Hello,world!");}

public void display () {System.out.println ("Welcome to the Java Programming");}

}

Mytest.java

Import Com.hank.www.HelloWorld;

public class MyTest

{

public static void Main (string[] args)

{

HelloWorld hw=new HelloWorld ();

Hw.display ();

}

}

Step 2 Create a directory folder under the C:/java folder to store the package, the path is: c:/java/com/hank/www, in this example, Helloworld.class is stored under the C:/java/com/hank/www folder.

Step 3 Compile the source file Helloworld.java, first should go to the source folder, that is, CD c:/java->javac–d C:/java Helloworld.java, the resulting helloworld.class is stored in c:/ The Java/com/hank/www folder.

Step 4 compiles the test file Mytest.java, which is Javac Mytest.java, which implements the creation and import of the package by running Java MyTest.

[note]: Before using the command javac–d <directionary>, you must first create the package path c:/java/com/hank/www in C:/java because the command does not automatically create the folder.

Reference:

Bruce Eckel <<thinking in java>>4-th Edition

Www.sun.com <<jdk1_5_0 documents>>

Www.chblogs.com/guenli/articles/1063102.html

Use of Java Packages

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.