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;
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.
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.
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';
Using two classes with same name in java
You have to create a new object with a full package as well:
import com.stackoverflow.FirstOne;
FirstOne ok = new FirstOne();
com.another.folder.FirstOne isthisOk = new com.another.folder.FirstOne();
Note: Case is important
Related Topics
Pyspark: Exception: Java Gateway Process Exited Before Sending the Driver Its Port Number
Java or Python for Natural Language Processing
Serializing with Jackson (JSON) - Getting "No Serializer Found"
How to Convert Camelcase into Human-Readable Names in Java
Compare Protocol in Swift VS Interface in Java
What Is the Equivalent of a Java Hashmap<String,Integer> in Swift
Passing a String by Reference in Java
How to Pass an Arraylist to a Varargs Method Parameter
What Java Xml Library Do You Recommend (To Replace Dom4J)
Aes/Cbc/Pkcs5Padding Encrypt in Java Decrypt in Ruby
Aes/Cbc Encrypt in Java, Decrypt in Ruby
Most Efficient Way to Cast List<Subclass> to List<Baseclass>
Tcp\Ip Client - Ehostunreach (No Route to Host)