Java packages and access rights, literacy stickers

Source: Internet
Author: User

When a large program is developed jointly by several different groups or people, it is possible to use the same class name. If this happens and you want to make sure that the program works correctly, you have to help by using the package keyword. The package is a measure to avoid duplicate names when using multiple classes or interfaces. So how exactly should you use it? Add the package declaration to the top row of the class or interface.

Package declaration format: the package name;

After the package declaration, the interfaces or classes within the same file are included in the same. The program TestPackage1 is an example of using the package, as follows:

Package Demo.java;class person{Public    String Talk ()    {        return ' person---->>talk () ';}    } Class testpackage1{public    static void Main (String args[])    {        System.out.println (new Person (). talk ());}    }

Line 1th declares a Demo.java package, so it is equivalent to placing the person class, the TestPackage1 class under the Demo.java folder. Now take a look at how the above program compiles:

Javac-d. Testpackage1.java Note: "-D" indicates the build directory; "." indicates that the build is in the current directory

This will generate a demo folder in the current directory, in the Demo folder will generate a Java folder, in this folder there will be compiled Person.class and Testpackage1.class, compiled with the following syntax to execute it:

Java demo.java.TestPackage1

If you want to use a class from a different package, you can specify the package name plus the class name as above "Demo.java.TestPackage1", or you can use the Import command. Imports can import a class or an entire package in a package, so it is no longer necessary to add a package name after using the classes in this package. Note that a class in a package must be set to public if it is to be accessed.

The following are the common packages in the JDK:

    1. java.lang-contains some of the core classes of the Java language, such as String, Math, Integer, System, and thread, to provide common functionality. There is also a sub-package in the Java.lang package: Java.lang.reflect is used to implement the reflection mechanism of the Java class.
    2. Java.awt-contains several classes that make up an abstract window toolset (abstract Windows toolkits), which are used to build and manage the graphical user interface (GUI) of the application.
    3. javax.swing-This package is used to establish a graphical user interface in which the components in this package are lightweight components relative to the java.awt package.
    4. The java.applet-contains some of the classes required for the applet to run.
    5. The java.net-contains classes that perform network-related operations.
    6. The java.io-contains classes that can provide multiple input/output functions.
    7. java.util-contains utility classes, such as defining system attributes, functions related to date calendars.

The access control permissions for Java Class members are listed below:

Java packages and access rights, literacy stickers

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.