« back

Packages in Java

27 Feb 2013

Special Delivery.... this post.(haha. Get it? Like delivering a package.)

Using packages in Java is a way to organize your classes into namespaces. You can compress packages into jar files. Doing so allows classes to be downloaded faster as a group vs. one at a time. Packages provide unique namespaces. It is recommended that when you are developing your own program, to group your program into a package. Java itself is a collection of packages.

If you are using an IDE, you may notice some of the "magical" benefits that it provides, i.e. all sorts of automatic creation for you. You may notice that when you use the IDE to create a class for you by only providing a name through the File new file diagalog, one of the things that it does is for you is populate your package name right at the top of your file. This package that is being included is the package that the class or classes within the file you have created belongs to. This is called package declaration. If you do not use package declaration, the class you created will be in an unnamed package. If a package has no name, it can not be imported from any other class in another package. Let's look at what a proper package declaration looks like. It is usually the first thing that you see in your file. For example...

1 package com.angeleah.webserver; <---- Here is the package line.
2 
3 public class BinaryReader {
4     ...
5 }

When you want to include different classes from java into your project, you can import the classes individually or can use a special syntax(*) that will include everything from the package.

1 package com.angeleah.webserver; 
2 import java.io.IOException; <---- This line will include only the IOException class from the io package. 
3 import java.io.*;   <----  This syntax includes everything from the io package.
4 
5 public class BinaryReader {
6     ...
7 }

If you want to avoid having to import the class, you can use it directly within your class by doing something like this...

1 java.io.BufferedReader exception = new java.io.BufferedReader();

So now that we see how you can include packages into your project, let's take a look at how we would properly name them. As usual, there are conventions to be followed.

The first time that I saw a package structure, I was really confused. The specification is there to avoid the possibility of two packages having the same name. I can get behind this but the way that it can be explained can be odd. The explanation usually assumes that an actual organization or company is responsible for creating the package as opposed to any random developer. With the rise of open source development, releasing software without being an actual company is ever more common and the conventions from the past can find themselves lost in translation. I will attempt to translate as best I can.

When creating your package, it is assumed that you are a company and that you own a domain name. The convention is that you should start your naming with a top level domain name of the organization.(in most cases, the .whatever_extension after a www.name_of_url) In my case, I am going to go with with com.

Then you want to list the organization's "domain" and any subdomains, in reverse order. In this case it will be angeleah. You can use your name as the organization name.

THEN you get to choose a name for your package. I am calling my project webserver. All together mine looks like this...

1 com.angeleah.webserver

At first, it seemed overblown but it really does add a layer of protection in naming collisions. If you are familiar with ruby, you can relate the idea of avoiding naming collisions to ruby namespaces and similar to this, the way you would pack up a ruby gem. Then just add a touch of java, and it all makes sense. ;)

Package delivered.

comments powered by Disqus