Forums Register Login

Wildcard imports may lead to ambiguities?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Catherine,

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

Steve
+Pie Number of slices to send: Send
Hi Steve
Yes, I don't understanding why it would be ambiguous.
1
+Pie Number of slices to send: Send
So when you reference just Node, which class does the compiler pick?
+Pie Number of slices to send: Send
I see... Thank you.
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
It also helps somebody else reading the code who might not know which Timer or other class you are referring to.
I was born with webbed fish toes. This tiny ad is my only friend:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 740 times.
Similar Threads
Importing Classes
Strange prob with Library
What is the disadvantage of using "*" in an import statement??
import pkg.* vs. import pkg.class
I am having a problem with import statements
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:49:52.