search
HomeJavaJavaBaseHow to use java package

How to use java package

Nov 15, 2019 pm 04:52 PM
javaBag

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!