3

I have two classes in my app with identicall names, I cannot rename them, on of them is from packageA second from packageB, the name of this class is State, and I have to use it in one place in my program like this:

 Map<Integer,Set<org.omg.PortableServer.POAManagerPackage.State>>  

is there any way (but using this class) to make this somewhat more readable(to shorten it somewhat)?

smallB
  • 16,662
  • 33
  • 107
  • 151
  • Duplicate of http://stackoverflow.com/questions/2447880/change-name-of-import-in-java-or-import-two-classes-with-the-same-name – Matthieu Napoli Jul 01 '12 at 10:12
  • Today I learned: you can't statically import a package, only a type. That was going to be my suggestion – Miquel Jul 01 '12 at 10:13

3 Answers3

4

Possibly derive from one of the classes to disambiguate. For example, in POAState.java:

import org.omg.PortableServer.POAManagerPackage.State;

public class POAState extends State {}

then:

Map<Integer,Set<POAState>> my_map;
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

Create wrapping class that will have only Set<org.omg.PortableServer.POAManagerPackage.State> and all the needed Set methods.

usage in client:

Map<Integer,GoodWrappingSetName> 
dantuch
  • 9,123
  • 6
  • 45
  • 68
0

If you use the two different State classes in the same piece of code (*.java file), then the answer is "No", Java does not provide a short hand notation. You must be explicit and include the full package names to remove the ambiguity.

@dantuch has raised an interesting idea, but rather than wrap the class, if you can extend one of them, you can create an empty sub-class of State that simply defers all of it's implementation to the parent class.

public MyState extends State {
    // no implementation required
}

Then you can then refer to MyState

Brad
  • 15,186
  • 11
  • 60
  • 74