Why do we need PACKAGES in java?

BOXIn our previous set of tutorials, you must have noticed the use of package and you have seen the import statements following the package statement in most of the classes. Yes, by now I expect you to have been familiar with these two keywords since I have briefly discussed them here but you probably don’t understand why we needed the packages, created the different packages in my last series of posts, put some classes within them and why I had to make call to the Import statement. Whatever the case, this post is to clear your doubts, if any, and make you understand why packages are important.

First, when you think about a package, what comes to your mind? For me, I think of a wrapped box with related Items – remember when you are checking out at the mall, the attendant packs related items together. For example, food items are not put close to detergents. Packages in Java does something similar to that of the mall attendant. It brings together related classes and interface into the same package so as to avoid mix up. For the sake of definition, a package is a collection of related classes and interfaces. To have a clearer view, let us assume families A, B, C and D with two children each with the profession shown in the illustration below:


Now, say Family D needs to build a house and he knows these other three families, all he needs to do is call all these families and employ their children for the different roles in the building.

This kind of scenario is similar to what happens in Java, note that:

  • The children in each family are related by blood.
  • Families A and B can have children with the same name and there won’t be any mix up since they will most likely have different surnames and other names.
  • Family D does not need to give birth to more children of its own with all needed profession to build a house.
  • Families A, B, C and D can decide to allow other families use the services of their children or make them usable only within their family.

The points above leads me back to our topic. Why then do we need packages?

  • To group related classes together hence providing convenience and order.
  • To separate concerns and avoid ambiguity.
  • To avoid repetition and allow re-usability using the Import
  • To control visibility and accessibility.

Now that we know the reasons why we need packages, let us see how to make use of it by answering the following questions:

  1. How do we create a package?

A package can be created simply by using the keyword Package. Refer to the package structure here and see example from our previous tutorial below:

package com.bee.code.blog.ShapeCommon;

  1. How do we name a package?

Java packages are named using the reversed internet domain name and lower case to avoid conflict with the names of classes or interfaces. E.g.

Domain Name                   Package Name Prefix
facebook.com                   com.facebook.mypackage
google.com                   com.google.mypackage
 gbonjubeeblog.wordpress.com  com.wordpress.gbonjubeeblog.mypackage

NOTE:  A package name could be anything, however, this is the standard naming convention and it is preferred in other to make programs more understandable by making them easier to read.

  1. How do we group classes into package?

There is no particular way. You only have to identify the classes that are related (condition for relationship is relative). See how I have categorized mine in our previous tutorial –


  1. What are the types of packages?

There are two types of Package in java which are: In-built and User defined packages.

In-built– These are packages that came with the Java API (Application Program Interface) and provides common classes used very frequently in coding. Some of the popular in-built packages and sample classes are listed below:

  • java.lang – System, String, Object, Thread, Exception, etc,
  • java.util – Date, Calendar, Currency, Linkedlist, Arraylist, etc ,
  • java.io – FileReader, FileWriter, RandomAccessFile, BufferedReader, etc,
  • java.awt – Button, Choice, TextField, Frame, List, Checkbox, etc,
  • java.net – URL, ServerSocket, Socket, etc.

User Defined– These are packages written and used by the developer himself. See the representation of the family illustration above – Package A, B, C and D


package A

 1 package A;
 3 /**
 4  *
 5  * @author 'beeCodeBlog
 6  */
 7 public class Bricklayer {
 9     public static void layFoundation() {
10     }
12 }


 1 package A;
 3 /**
 4  *
 5  * @author 'beeCodeBlog
 6  */
 7 public class Surveyor {
 9     public static void doSurvey() {
10     }
12 }

We have come to the end of today’s lesson, I hope I have been able to clear your doubt on  the why and how of Java package. In my next post, we will be looking at how to make a class callable outside its package, the import statement and the different styles of import. Till then, take time to familiarize yourself with packages and create your own. Happy coding!!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s