The Benefits of Java Namespaces – J025

by Jul 12, 2015

Try at home

  1. Make sure you understand Java namespaces
  2. Create a namespace hierarchy for an game. Just define the different namespaces you might need.
  3. Try creating errors in your program and compiling it

Transcript – The Benefits of Java Namespaces

Hey there! DJ Spiess here and we need to take a quick break from classes to talk about the organization of your Java application. If your Java program only has three classes, it’s very easy to manage and maintain. It’s all in one place. The number of classes in the average Java application can number in the hundreds. Sometimes well into the thousands. This can get overwhelming. You need a strategy for organizing you code. Java namespaces one part of this strategy.

In this lesson, we’re going to talk about namespaces, why we need them, and the best practices for creating and using namespaces in Java. We’ll finish up with some namespace examples in Java.

Java namespaces

When we start adding libraries from other programmers into our application, it’s likely that we’re going to use the same names. Simple concepts like a message, window, and sprite are likely to be named the same thing in everyone’s code. When you’re creating a class in Java, you need some way to tell Java, “I want this class in particular”.

This isn’t a problem unique to Java. Programming languages use something called a namespace. A namespace just draws a line around a collection of classes. The namespace defines a group or package of classes. When you ask for a particular class in Java, you specify the namespace. Java looks in the package that has the name of the namespace you give, and gets that particular class.

Java namespace naming conventions

You can give a namespace any name you like, but convention and best practices say to use the reverse of your domain name. So my domain name is, so I’d start all my namespaces with com.deegeu. That’s just the start.

What do you use if you don’t have a domain name? That’s a tough question. You want to use something that is globally unique, so don’t use anything that could be a url. Names aren’t the best choice either, unless you know no one else in the world has your name. However if you don’t plan on ever sharing your code, you could probably get away with firstname.lastname.

Java namespace organization

You can view namespaces as a tree or heirarchy. The domain is the namespace root for your classes. The next level should be the name of your project or application. You can then create more refined namespaces inside your namespace. Every branch can contain classes specific to that namespace. Every period denotes a new level in your namespace hierarchy. In this example, all the sprites of your space game would go in the namespace com.deegeu.spacegame.sprites.

Java namespace example - Java Namespaces - Free Java Course Online

A Java namespace example

You can refine as much as you want, but I would not refine any deeper than a module in a subsystem. The classes inside a package should either serve a single purpose, or they should serve a single purpose as a group. You could group all your sprite classes into one namespace, or you could group all your game drawing functionality in a namespace. You can even combine the two approaches.

You really are free to divide your program as you wish after the domain name, however, only create namespaces that make sense and contain classes. If you’re writing code to draw sprites in a game, you probably won’t need a database namespace.

You can refer to a namespace at any level in the heirarchy. We could have a namespace com.deegeu.animals.bird.aquatic which contains our Duck class. Maybe we want to refer to the namespace for all animals. Then we would use com.deegeu.animals.*. The asterisk is a wild card. It means “everything below this point”. com.deegeu.animals would contain all animals including our Duck class, but would not contain a Tree class in a com.deegeu.plants.tree namespace.

How do Java namespaces help us?

Namespaces make our Java class names longer, but how does that help us keep them unique? Java will not let you have two classes named the same thing in one namespace. It will let you use the same class name in two different namespaces. That’s how we make the classes unique. The real class name is the fully qualified name. And that should always be unique.

Arrays utility class - Java namespaces - Free Java Course Online

The Arrays utility class is in the namespace java.util.

Every class in Java is defined in a namespace, however, most classes in Java break the first rule of naming namespaces. Classes in Java are in the namespace java or javax. That’s the root. For example arrays. The namespace for arrays is java.lang.reflect. The fully qualified name for the array class is java.lang.reflect.Array. The array utilities we used in the array lessons are in the namespace java.util. The fully qualified name for the Arrays class is java.util.Arrays. When we print things out, we’re using the System class. It’s in the java.lang namespace and that class’s name is java.lang.System.

That’s how we organize classes logically into namespaces. I’ve showed you how we start every namespace with a reverse of our domain name. We’ve seen how to subdivide our namespace according to application function. And we’ve seen the full class name of several classes we’re already using. In the next lesson, I’ll show you how namespaces are represented physically in Java packages.

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • Background image from

Get the code

The complete video lesson list for this free online programming course can be found on the course syllabus page.

The source code for “How to override a method in Java” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!



deegeu-thumbs-small-0 4

Free Java Course Online

The “The Benefits of Java Namespaces” video is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This