Introduction to Packages Tutorial

Your computer contains thousands if not millions of files. These files are organized throughout folders (directories) and sub-folders (sub-directories). Can you imagine the mess if there were no such thing as folders and we had to have all of our files located on the root of our drive? Each file name must be unique, so we could only have one dog.jpg file and if our friend sent us a picture of their dog (dog.jpg) we would have to overwrite our dog.jpg to view their picture. Of course that is ridiculous, but in the world of Java everyone has created or a at least once. How many programmers do you think put the main method entry point in a class called

When it comes to your computer file system, a namespace is a group of related files that all have unique names. For example, c:\program files\java is a namespace where the JDK is installed by default in Windows. The namespace /usr/bin contains binary files for user programs in Linux. In our computer file system, we can also apply security to files and directories. Just like our computer file system, Java provides us with the ability to group our classes into packages. Packages also enable us to secure access to our classes and their members. As a matter of fact, package names must correspond to a directory structure on your computer.

Whenever I begin a new tutorial, I change to a c:\Java directory off the root of my drive. This is my workspace or working directory as I sometimes call it. I then create a new unique directory for each tutorial and that is where I create my classes. The purpose of the c:\java directory is purely organizational. I could have called it c:\blah or whatever. Throughout my tutorials I will use c:\java as my workspace, so think of this directory as a base of operations - all my tutorial stuff will by organized by package names under this folder.

If you are including a class in a package, then the very first statement must be the package statement. I will go over naming conventions in a future tutorial, but one of the most important rules is that the package name must be in lowercase. Therefore, for consistency, you should name your corresponding directories in lowercase as well. The package statement looks like this:
(package) (packagename) (;)
In this tutorial I will create a simple class that is part of an aboutme package and I will show you how to compile and run it properly.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md aboutme
C:\Java>cd aboutme

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

package aboutme; // must be the first statement in the class

class Hello {
    public static void main(String args[]) {
        String myName = "Dan";
        System.out.println("Hello everybody, my name is " + myName);

Now switch back to the command prompt (CMD) and type in cd .. and press Enter.
Now switch back to the command prompt (CMD) and type in javac aboutme\ and press Enter.
Now type in java aboutme.Hello and press Enter.

C:\Java\aboutme>cd ..
C:\Java\javac aboutme\
C:\Java\java aboutme.Hello
Hello everybody, my name is Dan

Final thoughts