Home> Java> javaTutorial> body text

What is a package in java?

青灯夜游
Release: 2019-11-15 17:15:52
Original
3377 people have browsed it

What is a package in java?

java What is a package?

In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names.Package is a container of classes, used to separate class name spaces.If no package name is specified, all examples belong to a default unnamed package.

In fact, it can be understood as a folder, and a tree directory storage method is used.

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.

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, and provide search and location classes, interfaces, enumerations, annotations, etc.

The syntax format of the package statement is:

package pkg1[.pkg2[.pkg3…]];
Copy after login

For example, the content of a Something.java file

package net.java.util; public class Something{ ... }
Copy after login

then its path should be net /java/util/Something.java is saved like this. The function of package is to classify and save different Java programs so that they can be called by other Java programs more conveniently.

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

The above is the detailed content of What is a package in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!