官术网_书友最值得收藏!

Packages

Classes are organized into packages and the first code line in a file should specify the package that the class is in.

package packt.java9.by.example.stringsort;

If you do not specify the package, then the class will be in the default package. This should not be used, except in the simplest case when you want to try some code. With Java 9, you can use jshell for this purpose, so, as opposed to previous versions of Java, now the suggestion becomes very simple—never put any class in the default package.

The name of the packages is hierarchical. The parts of the names are separated by dots. Using package names helps you avoid name collisions. Names of the classes are usually kept short and putting them into packages helps the organization of the program. The full name of a class includes the name of the package the class is in. Usually, we will put those classes into a package that are in some way related, and add something to a similar aspect of a program. For example, controllers in an MVC pattern program are kept in a single package. Packages also help you avoid name collision of classes. However, this only pushes the problem from class name collision to package name collision. We have to make sure that the name of the package is unique and does not cause any problem when our code is used together with any other library. When an application is developed, we just cannot know what other libraries will be used in later versions. To be prepared for the unexpected, the convention is to name the packages according to some Internet domain names. When a development company has the domain name acmecompany.com, then their software is usually under the com.acmecompany...  packages. It is not a strict language requirement. It is only a convention to write the domain name from right to left, and use it as package name, but this proves to be fairly good in practice. Sometimes, like I do in this book, one can deviate from this practice so you can see that this rule is not carved in stone.

When the rubber hits the road, and the code is compiled into byte code, the package becomes the name of the class. Thus, the full name of the Sort class is packt.java9.by.example.stringsort.Sort. When you use a class from another package, you can use this full name or import the class into your class. Again, this is on the language level. Using the fully qualified name or importing makes no difference when Java becomes byte code.

主站蜘蛛池模板: 铜陵市| 满城县| 马尔康县| 米泉市| 金平| 北安市| 彩票| 乳山市| 巢湖市| 育儿| 宜宾县| 庄河市| 广汉市| 津南区| 舞阳县| 临澧县| 互助| 淮安市| 乐安县| 澄迈县| 灌南县| 汤原县| 阿图什市| 丰台区| 观塘区| 临高县| 隆安县| 漳州市| 遵义市| 嘉黎县| 织金县| 略阳县| 榆树市| 石棉县| 汕头市| 特克斯县| 高碑店市| 兴和县| 宜兰县| 湖口县| 虞城县|