Importing Two Classes with Same Name. How to Handle

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.

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;

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 two exported classes with the same name

You can use as like this:

import {Class1} from '../location1/class1'
import {Class1 as Alias} from '../location2/class1'

You can find more about the ES6 import statement here.

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.

How to import two classes by the same name in javascript/es6?

Presumably component/Data and actions/Data both have default exports rather than named exports? Like this:

export default class Data {}

If that's the case, then the importer can call the variables whatever they like:

import Data1 from 'component/Data.js';
import Data2 from 'actions/Data.js';

If they are named exports:

export class Data {}

Then you need to use braces along with as to specify the source and target names:

import { Data as Data1 } from 'component/Data.js';
import { Data as Data2 } from 'actions/Data.js';

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.

Importing 2 different classes with same name and packages from 2 different modules

First this sounds like a design flaw to me cause having the same package names sounds that you haven't separated on the package level which you already did on the maven module level so not representing that same segregation level on the package level as well. This will usually happen if you have different groups of classes...apart from that If you have the exact same package and class name that is simply not possible cause a classname must be unique within a package which is violated here. The result will be having two different JAR's on classpath which offer the same class. The class which is really used depends on which JAR is first on the classpath which is by coincidence so not predictable.

OSGi will not help here cause you have the same problem here cause you would like to access the same class from different bundles which is not possible to solve. Furthermore in JDK 9 this will not work either cause those classes have to be made public (same as in OSGi) which will fail as well.



Related Topics



Leave a reply



Submit