HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
withjavanamesamethechangetwoclassesimport

Problem

In Python you can do a:

from a import b as c


How would you do this in Java, as I have two imports that are clashing.

Solution

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;

Code Snippets

import com.text.Formatter;

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

Context

Stack Overflow Q#2447880, score: 730

Revisions (0)

No revisions yet.