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
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()
orinstanceof ClassToRename
, etc. (example below adapted from a different answer)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).To be short, no, this isn't possible in Java.
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.