Concept of 3.Java Package

Source: Internet
Author: User

The "package" mechanism is unique in Java and is one of the most basic knowledge in Java. Some novice Java friends, often like learning other languages from the textbook "copy" some programs to run, but often encounter inexplicable error hints, these problems are in fact the "package" principle does not understand. This section will give an in-depth explanation of this issue.

What is a package in Java

In the Java programming language, for ease of development, multiple functionally similar classes are placed into a group, which is a "package", and the package is like a directory structure.

First, to observe the directory structure, the structure of the directory is divided into directories, subdirectories and files. In the operating system, how to represent the directory structure of a file? Let's look at an example of the file directory structure:
D:\Java\wp.doc
In fact, the package represents a bit like the directory structure of the file. For example: java.wp.

The following analysis of the above code, "Java" is the package name, and "WP" is the class name.

A package is a combination of some classes. For example, there are WP classes, WP1 classes, WP2 classes, and so on in a Java package.

The function of the package is summarized as follows:

    1. Packages organize files that contain class code to make it easy to find and use the appropriate classes.
    2. Packages are more than just containing classes and interfaces, they can also contain other packages. Form a layered package space.
    3. Helps avoid naming conflicts. When working with many classes, it is very difficult to ensure the uniqueness of class and method names. Packages can form hierarchical namespaces, narrow the scope of name collisions, and easily manage names.
    4. Control code access permissions.
How to implement a package

In Java to use a package, you must declare a package, and declare that a package must use the keyword "packages". The following is the case:
    Package java.wp
When declaring a packet, the declaration statement must be placed at the top of all statements in the class, starting with an instance.
public class package1{
    public static void main (string[] args) {
        System. Out.println (New Date ());
   }
}
Error editing, the system is prompted to find the class Date. Calling common classes in other packages in Java can be used in two ways:

    1. The first is to add the full package name before each class name. The following are the specific examples:
      Java.util.Date today=new java.util.Date ();
      Such an object instantiation looks very different from the previously used date Today=new date (), which is to precede the class name with the name of the package. Then change the section of the program just now, as shown below.
      public class package1{
      public static void Main (string[] args) {
      System.out.println (New Java.util.Date ());
      }
      }
      This time the compiler compiled the pass.
    2. The second way is by introducing a particular class. The introduction of a specific class in a class is implemented by the keyword "import", and the example above is still modified.
      Import Java.util.Date;
      public class package1{
      public static void Main (string[] args) {
      System.out.println (New Date ());
      }
      }
      This section of the program by introducing a particular class, the first class directly introduced, then the system compiles, the first call to this class, so compile without error.


When writing a program, it is not necessary to write the class to be introduced in detail, you can directly introduce all the classes in a particular package. For example, the following example.
Import java.util.*;
public class package1{
public static void Main (string[] args) {
System.out.println (New Date ());
}
}

What are classpath and default packages

When programming, the keyword "import" is not used to import packages and classes, which is why? Why do I have to import some times, and sometimes not import them?

When the Java virtual machine is running, the system will automatically import the "Java.lang" package, as long as the program uses the package's class, do not need to import, because the system is automatically imported for the programmer, as in this package to write a program segment. In addition to the "Java.lang" package, when you want to use a different package, you must import it manually.

Since this package is automatically imported by the system, this package is called the default package for the system.

What is a classpath? Before configuring the Java programming environment, the CLASSPATH is configured. The classpath is the ability to automatically let the system find the classes the programmer needs to import, so configuring the Classpath is critical in configuring the Java programming environment.

Scope of the package

In the preceding mentioned "public" and "private" access control, the class, method, or member variable declared as "public", can be used by any class, and declared as "private" class, method or member variable, can only be used by this class.

When no part of "public" or "private" is specified, it can only be accessed by all methods in this package and cannot be accessed by any method other than the package.

Concept of 3.Java 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.