• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wildcard imports may lead to ambiguities?  RSS feed

 
Catherine Fang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The followings are what I see just now.....
//******************************************
Ambiguities

Wildcard imports have one problem though: they can lead to ambiguities when classes with the same name exist in two packages you import via wildcard.

Imagine the following two imports:


import foo.*;
import bar.*;

Now you want to use the class foo.Node but there is also a class bar.Node. Now you need to use non-wildcard imports to resolve the ambiguity that would happen otherwise.
//*******************************************
But I can't understand it can bring problems. I think it is ok.
Thank you.
 
Steve Kedzie
Greenhorn
Posts: 22
Mac Netbeans IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Catherine,

I'm not really sure what you are asking here. Are you having trouble understanding why it would be ambiguous?

Steve
 
Catherine Fang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve
Yes, I don't understanding why it would be ambiguous.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when you reference just Node, which class does the compiler pick?
 
Catherine Fang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see... Thank you.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen that in real life: Somebody imported
import java.util.*;
import javax.swing.*;


and got a compiler error for the Timer class. Look up Timer in the API (lower left frame: alphabetical order) and you find it appears thrice. So the compiler needs disambiguation of Timers.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've gotten out of the habit of importing with wildcards and now I spell everything out explicitly. It helps to know exactly where an object is coming from.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also helps somebody else reading the code who might not know which Timer or other class you are referring to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!