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
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With