A brief discussion on package, import and import static

Source: Internet
Author: User

We may know that Java introduces the package mechanism, provides the class's multi-level namespace, resolves the class naming conflict, the class file management and so on. But there are always some problems.

problem a

The files in E:\Demo\java are: Zi.java, Test.java two files

The code in Zi.java is as follows:

public class Zi {public void print () {SYSTEM.OUT.PRINTLN ("abc");}}

The code in Test.java is as follows:

Import com. Zi;public class Test{public static void Main (string[] args) {new Zi (). print ();}}

Compile Zi.java in the DOS window, "javac-d. Zi.java ", afterwards. Found the Zi.class file in the current directory, so I created a new COM folder and put the class file in it

Then, compile Test.java, "Javac Test.java", compile error:

E:\demo\java>javac Test.java

Test.java:2: Could not access com. Zi

Wrong class file:. \com\zi.class

Class file contains the wrong class: Zi

Either delete the file or make sure the file is in the correct classpath subdirectory.

Import com. Zi;

Why is there such a mistake? The following explanations are given after finding the data:

Many beginners think that as long as the generated class file is placed in a directory, this directory name becomes the package name of this class. This is a false view, not having a directory structure, is equivalent to having the package name. Adding packages for Java classes must be specified in the Java source file through the package statement, which cannot be specified by the directory name alone. Therefore,the Java package mechanism requires two aspects of the guarantee: 1, the source file using the package statement to specify the packet name, 2, the class file must be placed in the corresponding path.

Question Two

How does import static work?

This is a static import syntax added after JDK1.5, which is used to import a static field, method, or all static field, method of a specified class. The static field, method referred to here is the Class property, the class method.

Import and Import static: You can omit the package name by using import, and you can omit the class names by using import static.

Import static Java.lang.system.*;import static java.lang.math.*;p ublic class test{public static void Main (string[] args) {//out is a static field of the Java.lang.System class, representing the standard output//pi is the static field of the Java.lang.Math class, which represents the π constant Out.println (pi);// Call the Sqrt static method of the math class directly out.println (sqrt (256));}}

This article is from the "Yunfei Dance" blog, please make sure to keep this source http://wuyunncu.blog.51cto.com/5978596/1600767

A brief discussion on package, import and import static

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.