Using other components in Java programs

Source: Internet
Author: User

Once you use a predefined class in your program, the compiler must know how to find it. Of course, this class may be in the same source file that makes the call. If that's the case, simply use the class-even if it's still not defined after the file. Java eliminates the issue of "forward referencing," so don't care about these things.

But what if the class is in another file? You might think that the compiler should be "federated" enough to find it yourself. But the truth is not so. Suppose we want to use a class with a specific name, but the definition of that class is in multiple files. Or worse, suppose we're going to write a program, but when we create it, we add a new class to our library, which conflicts with the name of a class that's already there.

To solve this problem, all potential and tangled situations must be eliminated. To achieve this, use the Import keyword to tell the Java compiler exactly what class we want. The import role is to instruct the compiler to import a "package"--or a "class library" (in other languages, to think of "library" as a collection of functions, data, and classes). Keep in mind, however, that all Java code must be written to a class.

Most of the time, we directly use components (parts) from the standard Java library, which are provided with the compiler. When using these components, there is no need to care about lengthy reserved domain names, for example, simply by writing a line of code as follows:
Import Java.util.Vector;
Its role is to tell the compiler that we want to use the Java vector class. However, Util contains a large number of classes, and we sometimes want to use several of them, and we don't want to explicitly declare them all. For this purpose, you can use the "*" wildcard character. As shown below:
Import java.util.*;
This approach is usually used when you need to import a series of classes. You should try to avoid importing classes one by one.

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.