Skip to main content
OCA Java Programmer 1Z0-803

Understanding Package and Import Statements in Java

By July 14, 2013September 12th, 2022No Comments

Packages in Java help us define classes within different directory structures that are accessible through the single CLASSPATH.

When creating code we can create an upper level directory, lets say c:java or c:code. When compiling or running code we can run the command from this directory or set the CLASSPATH environment variable to this directory.

We could of course ass all of our .java source files into this directory and compile the corresponding .class files into the single directory; however the in reality is that this would be impracticable as the amount of file grew and in an organisation this would make naming of file difficult to ensure uniqueness.

To overcome this issue packages exist in java. Packages represent a hierarchy within the file-system that then represents a hierarchy in the produces class file. Within the upper level directory c:java we can create a sub-folder representing the organisation you represent, in may case tup. Keep folder names to lower case. Within the tup folder we can further sub-folders perhaps c:javatuputils and c:javatupmessages to represent the purpose of corresponding code. Any java source file we create in the utils folder would begin with a package statement:

package tup.utils;

and the java source files in messages would begin with the statement:

package tup.messages;

The package statement is optional in java, if omitted then the code exists in the default package, this means that it will look for class files that are references within the current directory. This of course works but requires all code to be in the one directory. Using packages classes defines within the same package as the class being executed are available ; other classes can be made available that exist in a different package so long as the class is defined with public as its access modifier by using the import statement. For example to be able to access the ShowSystem class with in utils the class in messages would have the import statement:

package tup.messages;
import tup.utils.ShowSystem;

We may have seen the import statement in other java code before where we import classes from standard java utilities such as java.io or java.awt, we can now start using the same hierarchical mechanism to support our own Object Orientated code. To compiles the java files into class file we can, in my case, run the utilities from the c:java directory or any directory if we have the CLASSPATH variable or -cp switch set to c:java. To compile the ShowSystem.java file we would run the following command from the c:java directory:

javac tuputilsShowSystem.java

and the same with the Welcome.java file that references the ShowSystem class:

javac tupmessagesWelcome.java

To execute the class file when compiles we use the command

java tup.messages.Welcome

The videos steps you through the whole process from building java files that reference other class files and onto building packages.