javaimport

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


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;