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 package like in the Java class

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you what the package in the Java class is like, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Packages in the Java class.

Java applications are made up of several classes. But logically, these classes solve different problems. For example, some classes solve database access problems, and some classes solve security control problems. Just like the documents on our computer hard drive, we don't put them all in the root directory of a hard disk, but classify them according to our personal habits and put them in different folders. The same is true of classes in Java applications, which should also be logically classified and placed in different directories, which is the concept of packages.

Although the Java syntax itself does not require that a class be explicitly specified with a package name, the default package can be used. However, during the development of a Java project, any class should have a package. To specify a package name for a class, use the following syntax:

Package com.csst.db; public class CustomerDAO {}

As long as package is specified, after compiling CustomerDAO, the class file will exist in the com/csst/db folder. Physically, a package is actually a folder. But it's important to note that as long as a class specifies a package name, the package name is part of the namespace, that is, the name of CustomerDAO.class is actually com.csst.db.CustomerDAO, not CustomerDAO. Because of this, using this class, you have to specify the full name, such as:

Package com.csst.service; class CustomerService {private com.csst.db.CustomerDAO dao

Note: if the package name of the CustomerService is the same as the CustomerDAO package name, you do not need to specify the package name.

In practical applications, no one is willing to write such a lengthy name, usually using the import keyword to import classes from different packages.

Package com.csst.service; import com.csst.db.CustomerDAO; public class CustomerService {private CustomerDAO dao;}

Note: if you need to use many classes in the com.csst.db package at the same time, you can import multiple classes using the import com.csst.db.* statement.

In short, a package is to logically separate classes. When the Java class has the package in the Java class, it can not only avoid the problem of duplicate name (the package name is part of the class name, it is easy to ensure that the package name is different), but also use Java language modifiers for permission control.

The above is what the package in the Java class looks like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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