Home >Java >JavaBase >How to use java package

How to use java package

angryTom
angryTomOriginal
2019-11-15 16:52:493671browse

How to use java package

How to use the java package

During the development process, many classes will be defined. In order to avoid having the same class name In the event of overwriting, save all java programs in their own directories, and this directory is the package. The essence of a package is actually a folder.

1. Definition of package

A java program is composed of a public class and many non-public classes. Add "package name" to the first line of the java program. ;", you can save the *.class file in the corresponding package; when you need to call a class in this package, use the format of "package name.class name" to avoid conflicts with the same class name.

Note: When naming package names, try to avoid names that conflict with the system, such as java.lang, etc.; at the same time, package names are generally in lowercase.

Once the package name appears in the program class, when executing the program using the class name, you should try to bring the package name instead of entering the package for execution.

Note: In future project development, all .java files must use package definition packages.

2. Package import

Because there will be mutual calls between packages, a package will not only call classes in this package, but also call classes in other packages classes, so you need to use the keyword "import" to import classes in the package, format: import package name.class name;

When you need to import several classes in other packages, if It is troublesome to write them one by one. You can use the format: import package name.*; to complete the import.

Note: Using the wildcard character “*” does not mean that all classes in the package will be imported, but that all classes in the package will be imported according to requirements. Just import whatever classes are needed.

If different packages contain class names with the same name, you can use "package name.class name" to distinguish them.

For example:

cn.aaa.www.Demo  demo=new  cn.bbb.www.Demo();

Note: This code means generating an instantiation object of the Demo class in the cn.bbb.www package and assigning it to the Demo class in the cn.aaa.www package , the object reference is demo.

Code example:

package cn.aaa.www;
public class Hello {
    public void print() {
        System.out.println("cn.aaa.www包内的Hello类!");
    }
}

package cn.bbb;
import cn.aaa.www.Hello; //导入程序类
public class World {
    public static void main(String[] args) {
        Hello hello = new Hello();
        hello.print();
    }
}

Running result

cn.aaa.www包内的Hello类!

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of How to use java package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn