How to Import Two Classes With the Same Name in Different Packages

Importing two classes with same name. How to handle?

You can omit the import statements and refer to them using the entire path. Eg:

java.util.Date javaDate = new java.util.Date()
my.own.Date myDate = new my.own.Date();

But I would say that using two classes with the same name and a similiar function is usually not the best idea unless you can make it really clear which is which.

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

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.

Change Name of Import in Java, or import two classes with the same name

There is no import aliasing mechanism in Java. You cannot import two classes with the same name and use both of them unqualified.

Import one class and use the fully qualified name for the other one, i.e.

import com.text.Formatter;

private Formatter textFormatter;
private com.json.Formatter jsonFormatter;

How to use two class with the same name in different packages?

you will have to import one and other you will be writting fully qualified path

for example in your code:

import foo.bar.myClass;

.
.
.
myClass ob; // this will refer to foo.bar.myClass
foo.myClass ob1 ;//this will refer to foo.myClass

Same class name in different packages

Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements. You'll have to fully qualify one of the class names if you really need to reference both of them.


Example: Suppose you have

pkg1/SomeClass.java

package pkg1;
public class SomeClass {
}

pkg2/SomeClass.java

package pkg2;
public class SomeClass {
}

and Main.java

import pkg1.SomeClass;   // This will...
import pkg2.SomeClass; // ...fail

public class Main {
public static void main(String args[]) {
new SomeClass();
}
}

If you try to compile, you'll get:

$ javac Main.java
Main.java:2: pkg1.SomeClass is already defined in a single-type import
import pkg2.SomeClass;
^
1 error

This however does compile:

import pkg1.SomeClass;

public class Main {

public static void main(String args[]) {
new SomeClass();
new pkg2.SomeClass(); // <-- not imported.
}
}

Import classes with the same name

Importing two classes with the same name and then trying to use them won't work, because the compiler can ofcourse not know which one of the two classes you mean. You'll have to use the fully-qualified name of at least one of the classes instead of importing it.

For example:

package whatever;

import predictor.version1.Predictor;

public class Result {
// Uses the Predictor from package predictor.version1 which was imported
public void prediction(Predictor p) {
// ...
}

// Use fully-qualified class name to indicate that Predictor
// from package predictor.version2 should be used
public void prediction(predictor.version2.Predictor p) {
// ...
}
}

Import a class with same name as current class

You simply can't have two classes referenced by the same short name. So the solution is actually the same one as in the case of importing two classes with the same name:

You'll have to reference the other class by its fully qualified class name and your local one by its short name. You could even use the FQCN for both, if that helps readability in your case.



Related Topics



Leave a reply



Submit