Java learning ------ packaging, java ------ Packaging

Source: Internet
Author: User

Java learning ------ packaging, java ------ Packaging

First, let's talk about packaging. Why is package required? First of all, many functional classes will be used in java Development. there are classes defined by ourselves and there are also systems of their own classes. So what should I do when my defined class has the same name as the system class? As a result, the class ownership problem is defined. In this way, you can differentiate them. When we need to use a lot of tool classes, it is too troublesome and error-prone to import one by one. Therefore, the tool classes related to various functions are packaged. To use the tool class, you only need to import the corresponding package.

Next, how should we pack it? We use the keyword package to package all the classes in the file. Note: The package + package name must be in the first line. After packaging, the class name in the package will also be changed to a combination of package name + class name. After packaging, you need to create a package layer (that is, the file path) for the package, which can be manually created (just like creating a folder) or automatically created. Use the following command during compilation: javac-d. Class Name. The package layer is automatically generated.-d indicates that the package layer is generated in the current directory.

After the package is packaged, the class access in the package is involved.

Classes in the same package (same folder) can be accessed directly. However, private objects cannot be accessed or inherited.

For different packages, the package name must be added and only public classes (that is, public) can be accessed. to invoke methods in the class, this method must also be public ). Alternatively, use import to import the package, or only import the classes under the used package.

The above is the use of classes in the package.

Next we will talk about the jar package. The jar file has the following meanings: ①. Release and use the class library. ② Facilitates resource combination and management.

That is to say, integrate various tool classes. In addition, when other people need to use the class I defined, they can compress it into a jar package and hand it over to others.

So how to create a jar package? In the directory to be packaged, enter the following command: jar cvf jar package name. java *. In this way, you can compress it into a jar package.

What if you want to use the jar package? You can put the directory of the jar package into classpath. Because the system will first find the jar package in the current directory, if not, it will search in the classpath directory.

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.