Java Packages (Package)

Source: Internet
Author: User

Add an interface (interface) to the animals package:
Animal.java File Code:
/* File name: Animal.java */
Package animals;

Interface Animal {
public void eat ();
public void travel ();
}
Next, join the implementation of the interface in the same package:
Mammalint.java File Code:
Package animals;

/* File name: Mammalint.java */
public class Mammalint implements animal{

public void Eat () {
System.out.println ("Mammal Eats");
}

public void Travel () {
System.out.println ("Mammal Travels");
}

public int Nooflegs () {
return 0;
}

public static void Main (String args[]) {
Mammalint m = new Mammalint ();
M.eat ();
M.travel ();
}
}
Then, compile the two files and put them in a subdirectory called animals. Use the following command to run:
$ mkdir Animals
$ CP animal.class Mammalint.class Animals
$ Java Animals/mammalint
Mammal eats
Mammal Travel
Import keyword
In order to be able to use a member of a package, we need to explicitly import the package in a Java program. Use the "Import" statement to complete this function.
In the Java source file, the import statement should be located after the package statement, before all classes are defined, either without, or with multiple, syntax formats:
Import Package1[.package2 ...]. (classname|*);
If you melt Kim in a package, and one class wants to use another class in this package, the package name can be omitted.
Example
The following payroll package already contains the Employee class, and then adds a Boss class to the payroll package. The Boss class can refer to the Employee class without using the payroll prefix, as in the case of the boss class.
Boss.java File Code:
Package payroll;

public class Boss
{
public void Payemployee (Employee e)
{
E.mailcheck ();
}
}
What if the Boss class is not in the payroll package? The Boss class must use one of the following methods to refer to classes in other packages.
Use the class full name description, for example:
Payroll. Employee
Introduced with the Import keyword, using the wildcard character "*"
Import payroll.*;
Introduce the Employee class using the Import keyword:
Import Payroll. Employee;
Attention:
The class file can contain any number of import declarations. The import declaration must precede the class declaration after the package Declaration.
Directory Structure of the package
Classes are placed in a package with two main results:
The package name becomes part of the class name, as we discussed earlier.
The package name must match the directory structure in which the corresponding bytecode resides.
Here's an easy way to manage your own files in Java:
The source of the class, interface, and so on is put in a text, the name of this file is the name of this type, and with. Java as the extension. For example:
File name: Car.java

Package vehicle;

public class Car {
Class implementation
}
Next, place the source file in a directory that corresponds to the name of the package in which the class is located.
..... \vehicle\car.java
Now, the correct class name and path will look like this:
Class name, vehicle. Car
Path name, Vehicle\car.java (in Windows system)
Typically, a company uses its Internet domain name in reverse form as its package name. For example: Internet domain name is runoob.com, all package names start with Com.runoob. Each part of the package name corresponds to a subdirectory.
For example: There is a com.runoob.test package that contains a source file called Runoob.java, so the corresponding should be like the following series of subdirectories:
..... \com\runoob\test\runoob.java
At compile time, the compiler creates a different output file for each class, interface, and other type defined in the package, and the output file name is the name of this type, plus the. class as the extension suffix. For example:
File name: Runoob.java

Package com.runoob.test;
public class Runoob {

}
Class Google {

}
Now, we compile this file with the-D option, as follows:
$javac-D. Runoob.java
This will put the compiled file as follows:
. \com\runoob\test\runoob.class
. \com\runoob\test\google.class
You can import all the classes, interfaces, etc. defined in \com\runoob\test\ as follows:
Import com.runoob.test.*;
The. class file after compilation should be the same as the. Java source file, and the directories they place should correspond to the names of the parking. However, the path to the. class file is not required to be the same as the corresponding. Java path. You can arrange the directory of source code and class.
<path-one>\bjrongjinhuiyin.com\runoob\test\runoob.java
<path-two>\classes\com\runoob\test\google.class
In this way, you can share your class directory with other programmers without revealing your source code. Managing Source and class files in this way allows the compiler and the Java Virtual Machine (JVM) to find all the types used in your program.
The absolute path to the class directory is called Class path. Set in the system variable CLASSPATH. The compiler and the Java Virtual Machine construct the path to the. class file later by adding the package name to class path.
<path-two>\classes is the class Path,package name is Com.runoob.test, and the compiler and JVM will be in <path-two>\classes\com\runoob\ Test to find the. class file.
A class path may contain several paths, and multipathing should be separated by delimiters. By default, the compiler and the JVM find the current directory. JAR files are based on classes that contain Java platforms, so their directories are placed by default in class path.

Java Packages (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.