CIT 591 Problems with List
Fall 2002, David Matuszek

The Problem:

Some students are having problems with Lists, because both java.awt and java.util define a List class. Hence, if you say something like:

List myList = new List();

an error results because Java doesn't know which List you mean.

This is a more general problem, because you can name your classes anything you like. It will frequently happen that two or more classes have the same name; List is just the first time you have encountered the problem.

Solutions:

When you import your classes, you probably do something like:

import java.awt.*;
import java.util.*;

The * says to import all the classes in that package. You may not want them all; for example, you may not want the List class defined in java.util.*. Since you probably only want one or two classes from java.util, but many different classes from java.awt, it makes sense to import only the classes you want from java.util, like this:

import java.awt.*;
import java.util.Random;

You may find that you don't actually need any classes from java.util, in which case a better solution is not to import anything at all from java.util. It doesn't generally hurt to import classes you don't need, but in this case it gets in your way. Remember, import makes classes available to the code in the file that does the importing; if each of the (outer) classes you define is in a separate file (which is recommended in any case), you only need to import the Java classes that you need for that one class of yours.

If you do want to use both java.awt.List and java.util.List in the same class file, you must refer to them by their fully qualified names, for example:

java.awt.List myGuiList = new java.awt.List();
java.util.List myLinkedList = new java.util.List();

You only need to qualify the names of the classes; don't qualify the names of the variables (myGuiList and myLinkedList). Also, remember that if you use fully qualified names like this, you do not need import statements; the purpose of the import statement is to allow you to use simple class names, such as List instead of java.awt.List. In this example, if List is the only class that you want from java.util, you might do it this way:

import java.awt.*;
...
List myGuiList = new List();

java.util.List myLinkedList = new java.util.List();