Title: Declaration
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Declaration

Packages

To start out, it is necessary to define the concept of a Package. A package is an entity that groups classes and interfaces together. Packages perform the following functions:

- Organize and group classes, normally by functions. For example, the java.awt package contains the classes with graphical components.
- Help to define access to variables and methods. For example, by using the access modifier protected only classes in the same package will have access to it.
- Make it easy to import other programmer's classes into your code, while keeping the code separate from your own.
- If no package is explicitly declared, Java places your classes into a default package.

For the time being we will defer the topic of how to create a package. At that time we will go over the CLASSPATH concept which goes along with it.

In general, the Java compiler makes packages available to your program when you request the Package through an import statement. For example:
import java.util.Math;

One package that is always available to you without asking for it is:
java.lang

Take a look at the online Java help files to see how the help system is organized according to Packages.

Import Statements

As mentioned earlier, Java creates Packages for related modules. Let's look at the different ways that I can get at the Date class which lives in a package known as java.util. If I don't use an import statement, I would need to write my code as follows:


class Mytime
{
public static void main(String[] args)
    {
        java.util.Date now = new java.util.Date();
        System.out.println("Time = " + now);
    }
}


This seems like a lot of extra typing. This especially awkward when the number of classes in other packages is large. That's why you want to consider import statements. Here are 2 alternatives using import statements:


import java.util.Date;
class Mytime2
{
public static void main(String[] args)
    {
        Date now = new Date();
        System.out.println("Time = " + now);
    }
}


OR:


import java.util.*;
class Mytime2
{
public static void main(String[] args)
    {
        Date now = new Date();
        System.out.println("Time = " + now);
    }
}


java.util.* includes all of the classes in the java.util package.

Note that java.* does not do what you might think. The * doesn't recursively go down the package tree. The * only considers all of the classes in the top level tree node: java. By the way there are no classes pulled in by this import statement.

We will start out with a somewhat useful example using Packages. After covering this basic example, I want to defer the rest of the section until later in the course. I think it will make more sense to cover the details of this chapter after we have more Java under our belts.

At this stage, you may have noticed 2 things:

1. It's a pain in the xxx to type System.out.println everytime you want to generate output.
2. It would be nice to have the user type input through the keyboard.

We will solve item number one with a class called P. The second item will be solved with a class called MyInput. Now we could copy these classes to every directory where we want to work. But this seems like another bother. Alternatively, we could put these classes into a package called tools and import the package whenever we want to use this package.

Take a look at the tools Package.

By the way, for MyInput I'm going to sneek in some capability to read files as easily as reading the keyboard. We will use this later. Don't worry too much about the details inside of MyInput because will cover this when we cover Java I/O.

Here are the P.java and MyInput.java files that we are putting into the tools package. The main thing you need to see right now is the fact that both of these files start with the statement:
package tools;

The package statement must be the first executable statement.

Here is a test program that uses our tools package:


import tools.*;
class Test
{

    public static void main(String[] args)
    {
        String s;
        MyInput in = new MyInput();

        P.println("Enter your name:");
        s = in.get();   // read the keyboard
        P.println("echo: " + s);

        P.println("Enter your age:");
        s = in.get(); // read the keyboard
        P.println("echo:" + s);
    }
}


Here are the rules for making this work:

1. You need to set the CLASSPATH environment variable to contain the list of directories where packages can be found.
2. Hanging off one of the CLASSPATH directories we will need a directory called tools which will contain the classes in our tools package.
3. The client program needs an appropriate import statement. Test.java is importing all classes in tools. It could have individually imported each class with:
import tools.P;
import tools.MyInput;

I'm going to assume we are on Linux now, but there is a very similar mechanism for a DOS environment.

Create the Test.java program in some directory say xyz. Then inside of directory xyz, create a directory named tools. Put the P.java and MyInput.java files in the tools directory.

Set your CLASSPATH environment variable with the statement (don't miss the period):
export CLASSPATH=.

Execute your test program.

This still isn't convenient because we don't want to create a tools directory everywhere we do java work. The solution is to put the tools directory in some common place. I have placed the P.java and MyInput.java in a directory named /usr/local/jdk/classes/tools. So we can issue a statement like:
export CLASSPATH=.:/usr/local/jdk/classes

I'm actually going to recommend that the CLASSPATH you setup is :
export CLASSPATH=.:$HOME/classes:/usr/local/jdk/classes

This classpath will allow you to create a directory called classes in your home directory where you can store your own personal packages.

With this statement, your Test program will still preferrentially grab your P.java and MyInput.java from your local xyz/tools directory (which is nice because you can test out new versions locally).

You can now rename your tools directory and Java will find the Common P.java and MyInput.java in the /usr/local/jdk/classes directory.

Take a look at the my_profile we setup at the beginning of the class. Hopefully, it makes a little more sense now. .

Access Rules:

1. If something is labeled Public, it is available to the whole world of Java as long as you properly setup your CLASSPATH and include proper import statements.
2. If you remove Public from a method and let the access default, then if you are in the same package the method is accessable. However, if you are in a different package then the method is private. Note that all classes in the same directory are considered in the same package. Its kind of like there is always a default package consisting of all of the classes in your current directory.
3. If you make a method Private, then you can never access this method outside of the class --- even if you are in the same package.

Try some of these access rules in the MyInput class. Change the Constructor that takes one parameter, or the Open routine and test out the above rules.

We will cover the remainder of the chapter later

Last Updated: January 14, 2003 9:39 AM