Java Package and physical directory (reprint)

Source: Internet
Author: User
Tags naming convention uppercase letter

Original: http://www.cnblogs.com/moveofgod/p/3809653.html

Today, the JUnit experiment found that the same package name can be used under different physical directories (SRC, testsrc), and that in both directories there are subdirectories coolunit (this subdirectory is used in conjunction with the packages):

1. What is the relationship between the package name and the actual physical path?

A: The package name must appear as part of the actual path, only for a period of time and cannot be an absolute path. And the beginning of this paragraph is the one that contains the Classpath directory.

You can see the following code, the Eclipse auto-generated. classpath file, where classpath contains testsrc, SRC, and both directories have subdirectories Coolunit, which is the beginning of the package name.

<?XmlVersion= "1.0"Encoding= "UTF-8"?><Classpath><ClasspathentryKind= "src"Path= "src"/><ClasspathentryKind= "src"Path= "testsrc" /> <classpathentry Span class= "attr" >kind= "con" path= " org.eclipse.jdt.launching.jre_container/org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/javase-1.6 " Span class= "KWRD" >/> <classpathentry kind= "con" path= "ORG.ECLIPSE.JDT.JUNIT.JUNIT_CONTAINER/4" /> <classpathentry kind = "Output" path= "bin" /> </classpath>        

2. If the same package name can be stored under different physical paths, how does the Java compiler find the corresponding class?

A: Of course, it can be stored under a different physical path, except that part of the two paths must be the same, that is, the package name includes the part. How to find the Java compiler, simple, is to add the upper directory from the beginning of the package name to the Classpath, so that the corresponding class can be found. Similar to the above testsrc, SRC, both directories have been added to the classpath (of course, this work is done by Eclipse for us, if we compile ourselves, without the help of eclipse, we need to specify Javac–classpath to add these two directories)

3. It must be related to classpath, it is impossible for you to write a Java class, Java compiler can find, then how to introduce their own writing class?

How to reference your own class is to pass in the upper directory of the start of the package name of your class as the Javac–classpath parameter, so that the Java compiler can find out where your class is located.

Comprehensive: The package name is abstract out of the class space, although the physical storage needs folder with the hierarchy, but the key, this level is not an absolute path, just a part of the path, so we can completely put the code in different paths, as long as the path in the corresponding package name part of the same can be.

Package

To better organize classes, Java provides a package mechanism. A package is a container for classes that separates class name spaces. If you do not specify a package name, all examples belong to a default unnamed package.

Packages in Java generally contain related classes, for example, all classes on transport can be placed in a package named transportation.

Package Statement Format:

Package pkg1[. pkg2[. Pkg3 ...] ;

If there is a package statement in the program, the statement must be the first executable statement in the source file, preceded by a comment or a blank line. In addition, there can be at most one package statement in a file.

The name of the package has a hierarchical relationship, with points separating the layers. The package hierarchy must be the same as the file system structure of the Java development system (not an absolute path). Usually all lowercase letters are used in the package name, which differs from the naming convention in which the class name begins with an uppercase letter and the first letter of each word is capitalized.

When you use the package description, you do not need to reference (import) the same package or any elements of the package in the program. The import statement is used only to introduce classes from other packages into the current namespace. The current package is always in the current namespace.

If the file is declared as follows:

Package Java.awt.image

This file must be stored in the Java\awt\image directory of Windows or under the Java/awt/image Directory of UNIX.

The problem arises: because the path can not be absolute, that is, starting from the root directory down, so the starting position of the directory, only need to be the location of the classpath contained.

This is the key to solving the problem. In this sense, the package name is abstract

Java Package and physical directory (reprint)

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.