Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java, is there a way we can import a class under another name

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 
like image 511
Pacerier Avatar asked Apr 24 '11 00:04

Pacerier


People also ask

Can you import another class in Java?

You can import a specific class or the whole package. You place import statements at the top of your source files (but below any package statements). For example, you can import all classes in the java.

Can we import class from another package?

To import the java package into a class, we need to use the java import keyword which is used to access the package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file to refer to a class in another package by directly using its name.

Can we import two classes with same name Java?

If another class with the same name exists in another package, it will use the local class. Note that you can't import two classes with the same name in two different packages. The classes Integer and String belongs to the package java. lang but they don't need to be imported as the java.

Can we give fully qualified class name instead of importing that class?

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. It is generally used when two packages have same class name e.g. java.


1 Answers

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).

like image 192
Michael Borgwardt Avatar answered Oct 03 '22 02:10

Michael Borgwardt