tags:

views:

39

answers:

2

Suppose we have a package called com.example1 containing a Hello class (along with other classes).

Then we have another package com.example2 also containing a Hello class (obviously with different behaviour).

Now let's suppose we need every class in com.example1 and the Hello class in com.example2

import com.example1.*;
import com.example2.Hello;

Which one gets called in this case?

Hello hello = new Hello();

Or does this give a compile error?

This is just a theoretical question out of curiosity.

Since packages were created to avoid naming conflict, what happens when two packages contain two classes with the same name?

+3  A: 

It will give a compile error. You have to explicitly name the class - com.example2.Hello hello = new com.example2.Hello();

Petar Minchev
So there's no point in importing it?
klez
@klez Yeah, but you can leave the imports for clarity - just to be visible from where classes are imported at the top of the `java` file.
Petar Minchev
+2  A: 

Instead of leaving it to chance, it would be best to be explicit in your declarations. It is a compile error.

A similar clash often happens with java.util.List and java.awt.List. If you are explicit, there is no confusion.

Noel M
The java.awt.List collision has frustrated me to the point of removing it from my local development JRE.
mdma