I feel your pain, no matter what solution you use, the fact that there are two classes with the same name is rather confusing.
There are several solutions. :
- If this is your code, just rename one of them (or both)
- If this library (more likely) imports a more frequently used class, fully qualify the other, as suggested by Jeff Olson.
- Try to avoid them in the same class if possible.
- You can write your own
BarFoo and BazFoo that do nothing but extend their respective Foo classes, thereby providing them with their own names. You can define them as inner classes. Example:
private BarFoo extends com.very.long.prefix.bar.Foo{ //nothing, except possibly constructor wrappers } private BazFoo extends com.very.long.prefix.bar.Foo{ //nothing, except possibly constructor wrappers } class X { BarFoo a; BazFoo b; //... }
There are some disadvantages:
- You will need to override the constructors
- It will not be the same class if you need to pass it to a function that explicitly checks its
getClass .
You can solve these shortcomings by wrapping Foo classes rather than extending them, for example:
private BarFoo { public com.very.long.prefix.bar.Foo realFoo; } private BazFoo extends com.very.long.prefix.bar.Foo{ public com.very.long.prefix.baz.Foo realFoo; } class X { BarFoo a; BazFoo b;
Choose the simplest solution and good luck with it!
source share