Package Concepts in Java

Source: Internet
Author: User

Package packages in Java are the equivalent of folders on your computer. We usually in the work, the file too much, will be a new folder for classification management, Java package is similar to the truth, when our class too much, also need to manage. At this point we will put the class file in the package, that is, put the. class file into a folder, so that the class file can be classified management, but also effectively avoid naming conflicts.

When we compile the Java source file, it generates a. class file, and if we specify a package name (packages net;) at the top of the Java source file, at compile time, the package name will generate a folder, in this case the Net Folder, The compiled. class file is placed under the folder.

 Package net;   Specify package name netpublicclass  Test {    publicstaticvoid  Main (string[] args) {        System.out.println ("Hello");}    }

When the package name is very long, such as the packages net.com.cn, then it will generate multi-level folder, Mr. Net Folder, and then generate a COM folder in the Net Folder, and finally generate the CN folder in the COM folder, we compiled the generated. class file will be placed in the CN Folder. We write a animal class that specifies the package named net.com.cn

 Packagenet.com.cn; Public classAnimal {PrivateString name; Private intAge ;  PublicAnimal (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }}

After compiling, you will find our current folder more than a Net Folder, open Net Folder, found COM folder, and then find Open COM folder, found CN folders, in the CN file has our Animal.class class file. If you have a package, this class is actually called Net.com.cn.Animal, and the Animal class has its own, and if you create an object in the Java source file, you will use the

Net.com.cn.Animal cat = new Net.com.cn.Animal ();

When we use the class to create the object is very troublesome, so the import guide package appears. Direct in the class to be used, we can write the class name directly in the source code. We're writing a Test.java file.

import net.com.cn.Animal;  // Introduction Package  Public class Test {    publicstaticvoid  main (string[] args) {        new Animal ();  // Write directly using the class name        Cat.setname ("Miaomiao");        System.out.println (Cat.getname ());    }    }

After the introduction of the package, it is a class file after the ownership, which brings a permissions problem.  If the class file in the package can be accessed, it must be public, the method in the package, and if it can be accessed, it must also be public. The classes in the package are accessed, the class in the package being accessed must be public, and the method of the class in the package being accessed must also be the public permission.

Finally there is a jar package, it is a compressed file, when we compile, the class files are placed in the folder, then we compress the folder, the formation of a jar package, just like we on the folder, right click, generate WinRAR file. Because the compressed file size is small, it facilitates propagation. Third-party packages, which are published through jars, are directly imported into the jar package and can be used for the class files inside.

Package Concepts in Java

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.