Introduction to Java Packages

This tutorial provides an introduction to java packages and explains their features and benefits.

What are Java Packages?

Java packages are a way to logically and physically organize your code. Think of packages as a hierarchical folder structure within which you place your file. Let’s start with an example. In this example, we create a class that adds two numbers.

open up a text file and put the code below in the text file.

public class Addition {

	public static void main(String[] args) {
		Addition summer = new Addition();
		System.out.println(summer.add(args[0], args[1]));

	public double add(String x, String y) {
		return Double.parseDouble(x) + Double.parseDouble(y);

save and close the text file. Go to the folder that has the text file and type in

> javac
> java Addition 1 2

The first line compiles the java code into bytecode to produce a class file. The second command runs the class file in the JVM and produces the output on the command line.

This looks good so far, except for the fact that once you start creating more files, its going to be extremely difficult to manage them. Imagine you have 500 files in your project. (Real project would have much more). It, therefore makes sense to organize the files in folders or packages.

How to compile and run file in a java package?

We will now put the file in java package and see how it can be compiled and run. Go to the folder where you created the file. In there create the following folder structure
com->studytrails->maths. Here com is the parent folder, studytrails is its child and maths is the child of studytrails. Move the file inside the maths folder. After moving the file, open it up in a text editor and add this line on the top :

package com.studytrails.maths;

save and close. You have now moved the file to its own folder. As your system grows you can create separate folders and add new files there. For example, if you start writing printing code then you can create a folder structure of com.studytrails.printing. What this means is that you would be creating another folder called printing inside the studytrails folder and put all your printing files in there.

Compiling a file inside java packages

You can compile the file either from the folder that has the file or from the root folder that has the ‘com’ folder of you source code hierarchy. We recommend that you always go to the root folder since you would not be able to run the Addition code from inside the maths folder.

javac com/studytrails/maths/ 
java com.studytrails.maths.Addition 1 2

Notice that to run the class we use the dot notation. If you run from inside the maths folder you would get an exception

What are features or benefits of Java Packages?

We list some of the benefits or features of java packages. For people who are new to java, it might not all make sense, so ignore the below section if its difficult to follow.

  • Maven/Ant can use package based filters to include or exclude certain classes from the build.
  • As you will see in subsequent chapters, java has special permissions for classes in the same package.
  • packages help you organize code from different projects within a company. Its generally a good practice to have java packages that go like com.companyname.productname.*
  • packages help you separate the different tiers of your code base. For example all data access classes can go under com.companyname.productname.dao.
  • Its important that teams spend enough time on agreeing upon the package names and what kind of classes go in them. During code refactoring a developer should move a class if its in the wrong package

This finishes our conversation about packages. In the next tutorial, we will begin writing our first java class!

Leave a Comment