39

is there a way we can import a class under another name? Like if i have a class called javax.C and another class called java.C i can import javax.C under the name C1 and import java.C under the name C2.

We can do something like this in C#:

using Sys=System;

or Vb:

Imports Sys=System
Pacerier
  • 86,231
  • 106
  • 366
  • 634

4 Answers4

44

No, there is nothing like that in Java. You can only import classes under their original name, and have to use the fully qualified name for all that you don't import (except those in java.lang and the current class's package).

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
4

To be short, no, this isn't possible in Java.

Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149
2

No. I think Java deliberately ditched typedef. The good news is, if you see a type, you know what it is. It can't be an alias to something else; or an alias to an alias to ...

If a new concept really deserves a new name, it most likely deserves a new type also.

The usage example of Sys=System will be frowned upon by most Java devs.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • 7
    actually that's just an example. a better use case would be importing classes with clashing names (this will happen all the time) – Pacerier Apr 24 '11 at 01:02
  • 1
    @Pacerier name clash is not very common in java. and when it happens, use full class name with package name, whose purpose is specifically to resolve name clash. – irreputable Apr 24 '11 at 01:11
  • 6
    i do not believe that the purpose of packages is specifically to resolve name clashes. Packages are for grouping things logically. Name clash is "expected" in OOP, and that's why we have packages/namespaces – Pacerier Apr 24 '11 at 01:51
  • the #1 purpose of package is to prevent name conflicts. – irreputable Apr 24 '11 at 03:38
  • 15
    Name clashes are totally common in Java. So common that it is really annoying not to be able to rename imports. Node, Date, Display, Rectangle, Point, Vector, List, you name it. **I HATE TO WRITE `new qwerty.uiopasdf.ghjkl.yxcvbnm.qetuos.fhjlxvn.Node()`!!!111** – Matthias Apr 23 '14 at 13:17
  • I'd love it if anyone had actual history documented on this: >> "I think Java deliberately ditched `typedef`" – jocull Jul 05 '18 at 14:39
1

Java doesn't support static renaming. One idea is to subclass object in question with a new classname (but may not be a good idea because of certain side-effects / limitations, e.g. your target class may have the final modifier. Where permitted the code may behave differently if explicit type checking is used getClass() or instanceof ClassToRename, etc. (example below adapted from a different answer)

class MyApp {

  public static void main(String[] args) {
    ClassToRename parent_obj = new ClassToRename("Original class");
    MyRenamedClass extended_obj_class_renamed = new MyRenamedClass("lol, the class was renamed");
    // these two calls may be treated as the same
    // * under certain conditions only *
    parent_obj.originalFoo();
    extended_obj_class_renamed.originalFoo();
  }  

  private static class ClassToRename {
    public ClassToRename(String strvar) {/*...*/}
    public void originalFoo() {/*...*/}
  }

  private static class MyRenamedClass extends ClassToRename {
    public MyRenamedClass(String strvar) {super(strvar);}
  }

}
Robb Hoff
  • 1,719
  • 2
  • 17
  • 32