Java Learning------Packaging

Source: Internet
Author: User

First talk about packing. Why are you packing? First of all, the Java development process will use a lot of various functions of the class, have our own definition of the class, but also the system's own class. What happens when the class that you define has the same name as the system class? Then, you define the class's ownership of the package. This makes it possible to differentiate. And when we need to use a lot of tool classes, one import is cumbersome and error-prone. The relevant tool classes for various functions are then packaged. When you need to use a tool class, simply import the appropriate package.

Next, how to package it? We use the keyword package to pack all the classes in the file. Note here: The package+ package name must be on the first line. After packaging, the class name in the package will be changed accordingly, into the package name + class name combination. And after packaging, it is necessary to create a package layer (or file path) for a package that can be created manually (just like creating a folder), or it can be created automatically. When compiling, use the following command: Javac-d. Class name. The package layer is automatically generated,-D. Indicates that the package layer is generated in the current directory.

After hitting the package, it involves the access of the class in the package.

For the same package (same folder), the class can be accessed directly. However, the private cannot be accessed, nor can it be inherited.

For different packages, you need to add the package name and access only the common class (that is, public), and to access the methods in the class, the method is also public. Alternatively, import the package using import, or simply import the class under the package you are using.

The above is the use of packages already packaged in the class.

Next talk about the jar package. The existence of the jar file means that the ①, publishing, and using the class library. ②, easy to mix and manage resources.

That is, the integration of various tool classes. And when someone else needs to use a class that I define, they can make a jar package and hand the jar package to someone else.

So how to hit the jar bag? In the directory that needs to be packaged, enter the following command: Jar CVF jar Package name. java *. This will make it into a jar package.

What if I want to use a jar package? You can put the directory of the jar package into classpath. Because the system will look for the jar package in the current directory first, if not, it will be searched under the Classpath directory.

Java Learning------Packaging

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.