search
HomeJavaJavagetting StartedWhat is java package

What is java package

Nov 14, 2019 am 10:16 AM
java

What is java package

The java package is a container of classes and is used to separate class name spaces.

The role of packages

1. Organize classes or interfaces with similar or related functions into the same package to facilitate the search and use of classes. (Recommended learning: java course)

2. Like folders, packages also use a tree directory storage method. The names of classes in the same package are different, and the names of classes in different packages can be the same. When classes with the same class name in two different packages are called at the same time, the package name should be added to distinguish them. Therefore, packages can avoid name conflicts.

3. Packages also limit access permissions. Only classes with package access permissions can access classes in a certain package.

Java uses the package mechanism to prevent naming conflicts, access control, provide search and location of classes, interfaces, enumerations and annotations, etc.

The syntax format of the package statement is:

package pkg1[.pkg2[.pkg3…]];

A package (package) can be defined as a set of related types (classes, interfaces, enumerations and annotations), providing access protection and namespace management capabilities for these types.

Creating a package

When creating a package, you need to give the package an appropriate name. Later, if another source file contains classes, interfaces, enumerations or annotation types provided by this package, the declaration of this package must be placed at the beginning of the source file.

The package declaration should be on the first line of the source file. There can be only one package declaration per source file, and every type in this file applies to it.

If a package declaration is not used in a source file, the classes, functions, enumerations, annotations, etc. will be placed in an unnamed package.

Example

Let's look at an example that creates a package called animals. Usually use lowercase letters to avoid conflicts with class and interface names.

Add an interface to the animals package:

/* 文件名: Animal.java */
package animals;
 
interface Animal {
   public void eat();
   public void travel();
}

The above is the detailed content of What is 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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment