Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of the Java package

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the role of the Java package is what the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that you read the role of this Java package is what the article will have a harvest, let's take a look at it.

To better organize classes, Java provides a package mechanism to distinguish the namespaces of class names.

The role of the package:

1. Organize functionally similar or related classes or interfaces in the same package to facilitate the search and use of classes.

two。 Like folders, packages are stored in a tree directory. The class names in the same package are different, and the class names in different packages can be the same. When calling classes with the same class name in two different packages at the same time, you should add the package name to distinguish it. Therefore, packages can avoid name conflicts.

3. The package also limits access, and only classes with package access can access classes in a package.

Java uses packages (package) this mechanism is to prevent naming conflicts, access control, provide search and location classes (class), interfaces, enumerations (enumerations) and comments (annotation), etc.

The syntax format of the package statement is: package pkg1 [.pkg2 [.pkg3 …]]

For example, a Something.java file that contains its contents

Package net.java.util

Public class Something {

...

}

Then its path should be saved like this by net/java/util/Something.java. The function of package (package) is to classify and save different java programs, which is more convenient to be called by other java programs.

A package can be defined as a set of interrelated types (classes, interfaces, enumerations, and comments) that provide access protection and namespace management for these types.

Here are some packages in Java:

Basic classes packaged by java.lang-

Java.io- functions that contain input and output functions

Developers can package a set of classes and interfaces and define their own package. And it is worth advocating in the actual development, when you have completed the implementation of the class yourself, grouping the related classes can make it easier for other programmers to determine which classes, interfaces, enumerations and comments are relevant.

Http://www.iis7.com/a/lm/gjcpmcx/

Because package creates a new namespace (namespace), it does not conflict with any names in other package. Using the package mechanism, it is easier to implement access control and make it easier to locate related classes.

. . .

Create a package

When you create a package, you need to give the package an appropriate name. After that, if another source file contains the classes, interfaces, enumerations, or annotation types provided by the package, the declaration of the 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 each type in this file is applied to it.

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

Lowercase letters are usually used to avoid conflicts with class and interface names.

. . .

Import keyword

In order to be able to use the members of a package, we need to explicitly import the package in the Java program. This can be accomplished by using the "import" statement.

In the java source file, the import statement should be placed after the package statement, before the definition of all classes, there can be none or multiple, and its syntax format is: import package1 [.package2 …] . (classname | *)

If a class in a package wants to use another class in this package, the package name can be omitted.

Note: the class file can contain any number of import declarations. The import declaration must be after the package declaration and before the class declaration.

. . .

Directory structure of package

Class in a package has two main results:

1. The package name becomes part of the class name, as we discussed earlier.

two。 The package name must match the directory structure where the corresponding bytecode is located.

Here is an easy way to manage your own files in java:

Put the source code for classes, interfaces, and so on in a file whose name is the name of the type, with .java as the extension.

For example:

/ / File name: Car.java

Package vehicle

Public class Car {

/ / Class implementation

}

Next, put the source file in a directory that corresponds to the name of the package in which the class resides:.\ vehicle\ Car.java

Now, the correct class name and path will look like this:

Class name-> vehicle.Car

Path name-> vehicle\ Car.java (in windows)

Usually, a company uses the reverse form of its Internet domain name as its package name. For example, the Internet domain name is apple.com, and all package names start with com.apple. Each part of the package name corresponds to a subdirectory.

For example, the company has a com.apple.computers package that contains a source file called Dell.java, so there should be a series of subdirectories such as the following:

....\ com\ apple\ computers\ Dell.java

When compiling, the compiler creates a different output file for each type of class, interface, and so on defined in the package. The name of the output file is the name of this type, with .class as the extension suffix.

For example:

/ / File name: Dell.java

Package com.apple.computers

Public class Dell {

}

Class Ups {

}

Now, let's compile the file with the-d option, such as right: $javac-d. Dell.java

This places the compiled file like the one on the right:.\ com\ apple\ computers\ Dell.class.\ com\ apple\ computers\ Ups.class

You can import all the classes, interfaces, etc. defined in\ com\ apple\ computers\ as shown on the right: import com.apple.computers.*.

The compiled .class files should be the same as the .java source files, and the directory they place should correspond to the package name. However, the path of the .class file is not required to be the same as the path of the corresponding .java. You can arrange the source code and class directories separately.

\ sources\ com\ apple\ computers\ Dell.java

\ classes\ com\ apple\ computers\ Dell.class

In this way, you can share your class directory with other programmers without revealing your source code. Managing source code and class files in this way allows compilers and java virtual machines (JVM) to find all the types used in your program.

The absolute path to the class directory is called class path. Set in the system variable CLASSPATH. The compiler and the java virtual machine construct the path to the .class file later by adding the package name to class path.

\ classes is class path,package and the name is com.apple.computers, and the compiler and JVM look for .class files in\ classes\ com\ apple\ compters.

A class path may contain several paths. Multipaths should be separated by delimiters. By default, the compiler and JVM look for the current directory. JAR files contain classes related to the Java platform, so their directories are placed in class path by default.

. . .

Set the CLASSPATH system variable

Display the current CLASSPATH variable with the following command:

Windows platform (under DOS command line)-> C:\ > set CLASSPATH

UNIX platform (under Bourne shell)->% echo $CLASSPATH

Delete the current CLASSPATH variable contents:

Windows platform (under DOS command line)-> C:\ > set CLASSPATH=

UNIX platform (under Bourne shell)->% unset CLASSPATH; export CLASSPATH

Set the CLASSPATH variable:

Windows platform (under DOS command line)-> set CLASSPATH=C:\ users\ jack\ java\ classes

UNIX platform (under Bourne shell)->% CLASSPATH=/home/jack/java/classes; export CLASSPATH

This is the end of the article on "what is the function of the Java package?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the function of Java package". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report