Menu

Search

Saturday, 22 December 2012

Java Packages


          Packages in Java are used to better organize multi-file projects and for data protection. It’s not unusual for a project to have several hundreds of Java classes. Keeping them all in one directory is never a good idea. Hence the files will be located in various directories and sub-directories.

          What are the naming conventions for packages? Java developers use so-called reversedomain
name conventions. Let’s say you work on a project called Sales for a company called Acme, which has an Internet site at acme.com. Then every package name will start with the reverse URL ofthe company, followed by the project name: com.acme.sales.


         All Java classes that belong to this package would be stored in the following directory structure:
com/acme/sales.

          If some of the Java classes are specific to domestic sales, while others are used in international
sales, you will create two more sub-directories: com/acme/sales/domestic and com/acme/sales/
international.

           While directory names are separated by a forward slash or backslash, the corresponding Java packages are separated with periods. Java has a special keyword package, and its declaration has to be
the first line of the class (program comments don’t count). For example:

    package com.acme.sales.domestic;

          Let’s assume that you work for a company called Practical Java on project Lesson ; the name of the
package will be com.practicaljava.lesson, which is exactly what I’ve entered in the Package
field shown in Figure:-





No comments:

Post a Comment