Three. Java classes and Objects 8. Talk about Java Packages

Source: Internet
Author: User

In Java, for the convenience of organizing code, you can put functionally similar classes into a folder, this folder is called a package.

A package can contain not only classes, but also interfaces and other packages.

The directory is "\" to represent hierarchical relationships, such as E:\Java\workspace\Demo\bin\p1\p2\Test.java.

Package with "." To represent a hierarchical relationship, for example, P1.p2.Test represents a directory of \p1\p2\test.class.

How to implement a package

The package keyword allows you to declare a packet, for example:
Package p1.p2;
You must place the package statement in front of all statements, for example:

    1. Package P1. p2;
    2. Public class Test {
    3. public Test(){
    4. System. Out. println("I am the method of construction of the test class");
    5. }
    6. }

Indicates that the Test class is in the P1.P2 package.

Call of the package

In Java, there are two ways to invoke classes in other packages.

1) Precede each class name with the full package name

Examples of programs:

  1. Public class Demo {
  2. public static void main(String[] args) {
  3. Java. util. Date Today=new java. util. Date();
  4. System. Out. Println(today);
  5. }
  6. }

Operation Result:
Wed Dec 11:20:13 CST 2014

2) Introduce the class in the package via the import statement

Examples of programs:

  1. Import Java. util. Date;
  2. You can also introduce all the classes in the Java.util package
  3. Import java.util.*;
  4. Public class Demo {
  5. public static void main(String[] args) {
  6. Date Today=new date();
  7. System. Out. Println(today);
  8. }
  9. }

The result of the operation is the same as above.

In practical programming, it is not necessary to write the class to be introduced in detail, you can directly introduce all the classes in a particular package, such as import java.util.*;.

The path of the class

When you import a class, Java must know the absolute path to the class.

First create the Demo.java under the E:\Java\workspace\Demo\src\p0\ directory (E:\Java\workspace\Demo\src\ is the root of the project source file) and enter the following code:

  1. Package P0;
  2. Import P1. p2. Test;
  3. Public class Demo{
  4. public static void main(String[] args){
  5. Test obj = new test();
  6. }
  7. }


Then create the Test.java in the E:\JAVA\WORKSPACE\DEMO\SRC\P1\P2 directory and enter the following code:

    1. package p1.p2
    2. public class test {
    3. public test () {
    4. system.outprintln ( "I am the constructor of the test class"
    5. }
    6. }


Suppose we set the CLASSPATH environment variable to.;D: \ Program Files\jdk1.7.0_71\lib, source file Demo.java beginning with import p1.p2.Test; Statement, the compiler checks to see if there is a Test.java or Test.class file in the E:\Java\workspace\Demo\src\p0\p1\p2\ directory and continues to retrieve D:\Program if it does not exist files\ Jdk1.7.0_71\lib\p1\p2\ directory, the two directory does not exist will be an error. Obviously, Test.java is located in the E:\Java\workspace\Demo\src\p1\p2\ directory, compiler can not find, will error, how to do?

You can specify the Classpath through the classpath option of the Javac command.

Open cmd, enter the directory where the Demo.java file is located, execute the JAVAC command, and set the classpath to E:\Java\workspace\Demo\src Www.xingcaiyule.net as shown in:


When running a Java program, you also need to know the absolute path of the class, in addition to the path specified by the CLASSPATH environment variable, you can also increase the path through the Java command's classpath option, as shown in:


Note the difference between the Java command and the Javac command, the execution of the Javac command needs to enter the current directory, and the Execute Java command needs to go to the parent directory of the current directory, and the class name is preceded by the package name.

It can be understood that Javac is a platform command that operates on a specific platform file, indicating the path to the file being compiled. And Java is a virtual machine command, it on the class operation, that is, the description of the class to be in the form of a point of description, and can not add extension, but also note the case of the class name.

These commands are cumbersome, and actual development requires eclipse, which is handy for managing the package under Eclipse and compiling the run program. Eclipse is actually executing these commands.

Access rights for packages

A class, method, or member variable that is declared public can be used by any class under any package, and a class, method, or member variable declared as private can only be used by this class.

Classes, methods, and member variables that do not have any modifiers can only be accessed by all classes in this package and cannot be accessed by any class other than the package.

Three. Java classes and Objects 8. Talk about Java Packages

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.