Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.8. Packages

4.8. Packages
Last updated: 23 January 2013.

So far, the classes and interfaces that I have created in the previous tutorials were all in the same directory, which is fine for a small application with a few classes and interfaces. However, if you are developing a larger application containing many classes and interfaces, you will need to use packages in order to: A package is defined by its name and a corresponding directory. When you are developing an application, the first thing to do is to choose a base package name for your application. According to the Java code conventions, a package name must be written in lower case letters and should start with your company's internet domain name in reverse order. For example, the base package name for a tutorials' project at JavaPerspective would be:

com.javaperspective.tutorials

The next step is to use that base package name to create more specific package names in order to organize your classes and interfaces accurately. For example, you can create a package named com.javaperspective.tutorials.beginner for all the classes and interfaces that are used in the beginner section. Likewise, you can create packages named com.javaperspective.tutorials.intermediate and com.javaperspective.tutorials.advanced for all the classes and interfaces related to the intermediate and advanced tutorials respectively. Finally, the packages are:

com.javaperspective.tutorials
com.javaperspective.tutorials.beginner
com.javaperspective.tutorials.intermediate
com.javaperspective.tutorials.advanced

Now that you have package names, you need to create the corresponding directories accordingly because the directory structure of your application must reflect the package names. Hence, for Linux and Mac users, the required directories are:

/com/javaperspective/tutorials/
/com/javaperspective/tutorials/beginner/
/com/javaperspective/tutorials/intermediate/
/com/javaperspective/tutorials/advanced/

For Windows users, the file separator is the backslash character \. If you want to separate the source files (.java files) from the bytecode files (.class files), which is highly recommended, you need to create two directories in your application base directory (src for the source files and bin for the bytecode files) and copy the directory structure shown above in each of them. Both directories must contain the same sub directory structure:

/your_application_base_directory/src/com/javaperspective/tutorials/
/your_application_base_directory/src/com/javaperspective/tutorials/beginner/
/your_application_base_directory/src/com/javaperspective/tutorials/intermediate/
/your_application_base_directory/src/com/javaperspective/tutorials/advanced/

/your_application_base_directory/bin/com/javaperspective/tutorials/
/your_application_base_directory/bin/com/javaperspective/tutorials/beginner/
/your_application_base_directory/bin/com/javaperspective/tutorials/intermediate/
/your_application_base_directory/bin/com/javaperspective/tutorials/advanced/

Once you have created the necessary packages, you can create classes and interfaces according to your needs. The class containing the main method must be easy to find since it is the starting point of your application. Personally, I tend to put it in the base package but you may well create a special package named com.javaperspective.tutorials.main and put it there. What matters is that it should be easy to find. Next, distribute the other classes and interfaces among the other packages according to their purpose. A class or interface that belongs to a given package must reside in the corresponding directory. For example, the class Aircraft shown below belongs to the package com.javaperspective.tutorials.beginner and therefore, must be saved in the directory /src/com/javaperspective/tutorials/beginner/:

package com.javaperspective.tutorials.beginner;

public class Aircraft {

   
public Aircraft(){
         
System.out.println("Aircraft created");
   
}

   
// ...
    // ...
}

The keyword package declares that the class Aircraft belongs to the package com.javaperspective.tutorials.beginner. The package declaration can be preceded by comments but it must be the very first statement of the class.

Similarly, the class App containing the main method belongs to the base package com.javaperspective.tutorials and must therefore be saved in the directory /src/com/javaperspective/tutorials/:

package com.javaperspective.tutorials;

public final class App {

   
public static void main(String[] args) {
         
com.javaperspective.tutorials.beginner.Aircraft aircraft = new com.javaperspective.tutorials.beginner.Aircraft();
   
}

}

As you can see, the class Aircraft is accessed by its fully qualified name (com.javaperspective.tutorials.beginner.Aircraft) because it does not belong to the package of the current class and therefore, is not visible without its fully qualified name. Instead of writing the fully qualified name, you can import the class Aircraft with an import statement as shown below:

package com.javaperspective.tutorials;

import com.javaperspective.tutorials.beginner.Aircraft;

public final class App {

   
public static void main(String[] args) {
         
Aircraft aircraft = new Aircraft();
   
}

}

Import statements must take place after the package declaration and before the fields and methods declarations. As you can see, I only imported the class Aircraft but I could have imported all the classes and interfaces of the package com.javaperspective.tutorials.beginner this way:

import com.javaperspective.tutorials.beginner.*;

Similarly, the following import statement imports all the classes and interfaces of the package com.javaperspective.tutorials:

import com.javaperspective.tutorials.*;

It is important to realize that the import statement above does not automatically import the classes of the sub packages com.javaperspective.tutorials.beginner, com.javaperspective.tutorials.intermediate and com.javaperspective.tutorials.advanced. Consequently, if in a given class, you import the entire package com.javaperspective.tutorials as I did above, you cannot use the classes belonging to the sub packages com.javaperspective.tutorials.beginner, com.javaperspective.tutorials.intermediate and com.javaperspective.tutorials.advanced unless you import those sub packages explicitly or use fully qualified class names.

The Java packages allow you to manage your name space with increased flexibility. You can create two classes that have the same name in different packages. For example, you can create another class named Aircraft in the package com.javaperspective.tutorials.intermediate (naturally, the new file Aircraft.java must be saved in the directory /src/com/javaperspective/tutorials/intermediate/). In that case, if you want to use com.javaperspective.tutorials.beginner.Aircraft and com.javaperspective.tutorials.intermediate.Aircraft in the same class, you must use fully qualified names to avoid a naming conflict. As an example, let's create a class named Aircraft in the package com.javaperspective.tutorials.intermediate:

package com.javaperspective.tutorials.intermediate;

public class Aircraft {

   
public Aircraft(){
         
System.out.println("Intermediate Aircraft created");
   
}

   
// ...
    // ...
}

The class App shown below now uses com.javaperspective.tutorials.beginner.Aircraft and com.javaperspective.tutorials.intermediate.Aircraft:

package com.javaperspective.tutorials;

import com.javaperspective.tutorials.beginner.*;

public final class App {

   
public static void main(String[] args) {
         
Aircraft aircraft = new Aircraft();
          com.javaperspective.tutorials.intermediate.Aircraft aircraft2 =
new com.javaperspective.tutorials.intermediate.Aircraft();
   
}

}

On the one hand, the variable aircraft1 belongs to the package com.javaperspective.tutorials.beginner and its fully qualified name is not used because the import statement is done. On the other hand, the variable aircraft2 is declared with a fully qualified name to avoid a naming conflict. Without the fully qualified name, the type of the variable aircraft2 would be com.javaperspective.tutorials.beginner.Aircraft.

To compile the classes, open a terminal (Linux and Mac users) or a command line prompt (Windows users), change to the src directory and type the following command:

javac com/javaperspective/tutorials/App.java -d ../bin/

The -d option is to tell the compiler that the bytecode files must be generated in the bin sub directories. As a result, each bytecode file is generated in the right sub directory:

/bin/com/javaperspective/tutorials/App.class
/bin/com/javaperspective/tutorials/beginner/Aircraft.class
/bin/com/javaperspective/tutorials/intermediate/Aircraft.class

In order to run the program, you need to change to the bin directory and type the following command:

java com.javaperspective.tutorials.App

The output is:

Aircraft created
Intermediate Aircraft created

As you can see, the compilation and execution are a bit awkward. However, IDEs like Eclipse just do all the work for you as you will see in the First project with Eclipse tutorial.

All things considered, understanding what packages are and how they work is important but in the beginner section, creating packages to run the examples is not necessary as I will use a small number of classes and interfaces.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.8. Packages
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.9. Access modifiers

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7