J2EEOnline J2EE  

Packages and Interfaces  «Prev  Next»
Lesson 2Understanding packages
ObjectiveExamine Packages and the Role they play in Java Class Organization.

Packages and the Role they play in Java Class Organization

To aid in the organization of classes, Java provides a construct known as a package that is used to organize classes into functionally-related groups. Packages do not directly impact the internal makeup of a class but they do impact the relationship between classes.
  1. All of the classes in the Java API are organized into a group of packages.
  2. The standard java.lang package is the only package that is automatically imported into every Java program.
  3. Packages help you uniquely identify classes. The package to which a class belongs is implicitly part of the class name.
    So the standard Java class Graphic is really named java.awt.Graphics. This identification helps to provide structure to all Java classes.

Below are some guidelines for package naming and structuring:
Follow these java package naming conventions:
  1. Structure your packages according to their functional role as well as their business role
  2. Break down your packages according to their functionality or modules. for example com.course.module.lesson
  3. Do not created too many packages if you have fewer classes in the package
  4. Avoid going overboard with structuring, avoid separate packaging for exceptions, factories, etc. unless you have to .
  5. If your project is small, keep it simple with few packages. e.g. com.course.module.lesson1 and com.course.module.lesson2
  6. Consider build and distribution when naming ( this will allow you to distribute your api or SDK in a different package, see servlet api)