Java Basic Learning--12, package

Source: Internet
Author: User

We have written some Java programs. Each of the previous Java programs was saved as a file, such as Test.java. The program is then compiled to Test.class. We end up using $java test to run the program.

However, in a normal Java project, we often need to write more than one. Java program, the final Java product includes all Java programs. Therefore, Java needs to solve the problem of organizing Java programs. Package is designed to better organize Java programs.

the establishment of the package
 Packagecom.vamei.society; Public classhuman{/*** Constructor*/     PublicHuman (inth) { This. Height =h; System.out.println ("I ' m Born"); }    /*** Accessor*/     Public intgetheight () {return  This. Height; }    /*** Mutator*/     Public voidGrowheight (inth) { This. Height = This. Height +h; }    Private intheight;}

The first line of the above statement

Package com.vamei.society;

Indicates that the program is in the com.vamei.society package. Com.vamei (Vamei.com's anti-write) represents the domain name of the package author (unfortunately, this domain name has already been registered by others, so it only plays the role of demo). Java requires the package to have a domain name prefix in order to differentiate between different authors. The society is a further local path name. Com.vamei.society together make up the name of the package.

The package provides a namespace for Java programs (name space). The full path of a Java class is composed of its package and class name, such as Com.vamei.society.Human. The corresponding Human.java program should be placed under the com/vamei/society/. Classes are identified by the full path, so different packages can have classes with the same name, and Java is not confusing. For example, Com.vamei.society.Human and Com.vamei.creature.Human are two different classes.

Look at one more detail. The human class is public and its construction method is public, so any other object can call the class. As we said before, there can be only one public class in a Java file, and that class will go to the. java file with the same name. A class can have no public keyword, and it actually represents a permission: The class is visible in the package in which it resides . That is, other Java programs in the package can access the class. This is the default access permission in Java.

Similarly, the members of an object can be default permissions (visible in the package). For example, we remove the public keyword in front of the GetHeight () method.

Call of the package

All we need to do is put the Human.java compiled human.class into the appropriate folder. For example, I put Human.class into the com/vamei/society/. In fact, you can also put the. java file into the appropriate path, and Java will automatically compile when it is used.

If the entire package (that is, the COM folder) is in the current working path , you do not need a special setting to use the package, such as the following testagain.java:

import com.vamei.society.*;  Public class testagain{    publicstaticvoid  main (string[] args)    {         New Human (+);        System.out.println (Aperson.getheight ());}    }

Import is used to identify the path. With the import statement, we can introduce classes under the corresponding path. * Indicates the introduction of all classes under the Society folder. In Testagain, we used the human class directly.

We can also provide the complete path to the class, which distinguishes between classes with the same name but different paths, such as:

 Public class testagain{    publicstaticvoid  main (string[] args)    {        =                   new Com.vamei.society.Human (.);        System.out.println (Aperson.getheight ());}    }

Because we provide the complete classpath, we do not need to use the import statement.

If the package is not placed under the current working path , we need to notify Java when we use the package. For example, we put the package in/home/vamei/javapackage so that Human.class is located in/home/vamei/javapackage/com/vamei/society/ Human.class, and our working path is/home/vamei. This way, the package cannot be found. One approach is to use-classpath to describe the folder path where the package is located when using Javac and Java, such as:

$javac-classpath/home/vamei/javapackage:. Testagain.java

$java-classpath/home/vamei/javapackage:. Testagain

is from/home/vamei/javapackage and working path (.) In the search package. The human class can be found in Java from/home/vamei/javapackage, and the Testagain class can be found from.

Alternatively, you can set the system's CLASSPATH environment variable to add the above path to the variable without typing the-classpath option each time.

Package-like mechanisms are also common in other languages, such as the import mechanism in Python. They are all designed to better organize and use existing programs. Using the package, we can easily expand the Java program, using the existing Java library. Note that the package is managed by a . class file. Java is known as "compile once, run everywhere" (Compile Once, run anywhere). the. class file can be used in any fitted Java Virtual Machine running on the platform (JVM, Java Virtual machine), which helps us overcome the difficulty of porting the system.

The difference between systems can be very large. If we write a program in C, we need to recompile the source program on each platform to accommodate different hardware conditions. The Java Virtual machine joins the platform and the Java universe, which forms the middle layer of hardware and programming logic. The JVM hides the hardware differences and provides the programmer with a "standard" Java universe. The. class file can be seen as a circulating currency in the Java universe. In the JVM infrastructure, plus package management assistance, Java programs achieve good portability (portability).

Java Basic Learning--12, package

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.