29

I have imported an Existing Java Application into my Workspace . I see that , a class with same name is present in different packages with in the Application.

For example a class named "Status.java" is present with in

com.tata.model.common.Status;
com.bayer.frontlayer.dao.Status;

When I tried to use both of them within a class, for example as shown below

import com.tata.model.common.Status;
import  com.bayer.frontlayer.dao.Status;
public class Adapter
{

}

It started giving an error in Eclipse stating

The import com.bayer.frontlayer.dao.Status collides with another import statement

Is there anyway to solve this without changing the name of the classes??

Thank you.

Dimitar
  • 4,402
  • 4
  • 31
  • 47

3 Answers3

38

You can use them explicitly without importing them, so the included package name differentiates between the two:

 //No imports required!
public class Adapter
{
     private com.tata.model.common.Status x;
     private com.bayer.frontlayer.dao.Status y;
}
  • 1
    There is not a less verbose way? How do people end up getting to reference it via only the last couple of packages for example common.Status instead of com.tata.model.common.Status – pete May 27 '15 at 18:42
  • @pete can you give an example? – nasukkin Sep 01 '16 at 20:42
13

You can import just one of the classes and use the fully qualified name for the other one.

e.g.

import com.tata.model.common.Status;
//import  com.bayer.frontlayer.dao.Status;

class SomeClass{
    void someMethod(){
       new Status(); //  com.tata.model.common.Status
       new com.bayer.frontlayer.dao.Status(); //com.bayer.frontlayer.dao.Status
    }
}

Though I think it would be less confusing in your case if you just used the fully-qualified names for both classes.

Vlad
  • 18,195
  • 4
  • 41
  • 71
  • 1
    There is not a less verbose way? How do people end up getting to reference it via only the last couple of packages for example common.Status instead of com.tata.model.common.Status – pete May 27 '15 at 18:42
  • If you own any of the packages, you can rename of the classes. If they implement any other interfaces that are enough, you can declare your variables based on one of the interface types. You could also wrap the objects, but that would do way more damage than good. Other languages let you [rename](http://stackoverflow.com/a/28362090/469220) imports. – Vlad May 28 '15 at 11:06
3

Directly apply full Class Names wherever applicable. Eg-

public class SomeClass {

public someMethod() {

com.myapp.someotherpackage.Status = "something";

com.some.other.package.Status = "otherthing";

if(com.myapp.someotherpackage.Status == com.some.other.package.Status) {

}
....
}
}
adarshr
  • 61,315
  • 23
  • 138
  • 167
Acn
  • 1,010
  • 2
  • 11
  • 22
  • Thank you all very much for helping , but unfortunately i could tick on to only one answer . –  Jan 11 '12 at 09:51