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.
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.
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 deegeu.com, 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.
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.
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.
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.
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.
Hey! Thanks for watching the video.
There is a quick quiz for this on DeegeU.com if you’d like to gauge how much you learned.
If you like the videos you are seeing please let me know by liking the video and hitting
the subscribe button for the DeegeU channel on YouTube. I’d really appreciate that.
If you have concerns or questions please leave them in the comments below or on DeegeU.com.
There’s a poll on the front page of DeegeU.com so you can let me know what topic is covered
next.
Thanks for watching and see you in the next video!