How to import two classes with the same name in different packages?

Ment picture Ment · Sep 16, 2010 · Viewed 8.3k times · Source

I want to import these two classes, both named Query - one a JDO class, the other a JPA class, to use in different methods in the same class.

import javax.jdo.Query;
import javax.persistence.Query;

Is there a way to globally import both of them at the same time at the top of the file?

Answer

Nikita Rybak picture Nikita Rybak · Sep 16, 2010

I'm afraid, no. But you don't have to import class to use it: just reference one of the classes by its full name, like

javax.jdo.Query query = getJDOQuery();
query.doSomething();

Then you can import another without name collisions.

BTW, sometimes if you start getting lots of such name such collisions in your class, it's a subtle hint for refactoring: splitting functionality of one big class between several small ones.