0

I use a mass import class group as below:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.text.*;
import java.util.regex.*;

This way I don't have to worry about leaving any classes out.

Then when I used List I got this error:

reference to List is ambiguous, both class java.util.List in java.util and class java.awt.List in java.awt match

How do I fix this? Is there a way to deport one of the classes? Which one is better?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Barakados
  • 371
  • 7
  • 19

4 Answers4

3

You will need to refer to these types with their fully qualified names, java.util.List and java.awt.List, throughout this class file.

This will disambiguate the two Lists so that it won't strictly be necessary to "deport" anything (your import statements can stay the same). However I do agree with the other answer's recommendations that you tidy up your imports.

If one of the Lists was "deported" then that would let you refer to the other List by its short name.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
2

Firstly, you shouldn't import whole packages like that. You should include only what is required. IDEs do have options where they will include for you whatever is required. At least Eclipse does that. For reading its disadvantages in detail you can go through the following link:

However, if it is absolutely necessary then ambiguity can be avoided by referring to these ambiguous classes with their fully qualified names e.g., in your case

  • java.util.List AND/OR java.awt.List
Community
  • 1
  • 1
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
0

The fix is to not import everything like that. Use an IDE like Eclipse to manage your imports for you.

Marvo
  • 17,845
  • 8
  • 50
  • 74
0

always and always import only the classes that you use in your particular program. For classes with the same name but different package like the List class that you are using, can be specified with their fully qualified names rather than the import statement

Edge
  • 722
  • 2
  • 7
  • 21