Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
While other JVM languages, such as Groovy, Kotlin and Scala have Python-like import aliasing, Java does not. It's not possible to 're-name' imports, however, when you call a class from a package, ...
Answer
#3: Post edited
Java 8 does not have a method of aliasing, or 're-naming', imports. When you call a class from a package, you can give the full package name before the class name.- For example, when calling `FooClass.methodName()` after importing a different copy of `FooClass`, `com.company.utils.FooClass.methodName()` specifies the `FooClass` in the `com.company.utils` package. This allows you to reference multiple copies of `FooClass`.
- Full example:
- ```java
- import com.company.special.FooClass;
- class BarClass {
- public void exampleMethod(){
- //Call method1 from FooClass that we imported above
- int i = FooClass.method1();
- //Call a class named FooClass that is in a different package
- String str = com.company.utils.FooClass.methodName();
- }
- }
- ```
- While other JVM languages, such as [Groovy](https://groovy-lang.org/structure.html#_import_aliasing), [Kotlin](https://kotlinlang.org/docs/packages.html#imports) and [Scala](https://docs.scala-lang.org/tour/packages-and-imports.html#imports) have Python-like import aliasing, Java does not.
- It's not possible to 're-name' imports, however, when you call a class from a package, you can use its FQN (Fully Qualified Name), that is, add the full package name before the class name.
- For example, when calling `FooClass.methodName()` after importing a different copy of `FooClass`, `com.company.utils.FooClass.methodName()` specifies the `FooClass` in the `com.company.utils` package. This allows you to reference multiple copies of `FooClass`.
- Full example:
- ```java
- import com.company.special.FooClass;
- class BarClass {
- public void exampleMethod(){
- //Call method1 from FooClass that we imported above
- int i = FooClass.method1();
- //Call a class named FooClass that is in a different package
- String str = com.company.utils.FooClass.methodName();
- }
- }
- ```
#2: Post edited
Java 8 does not have a method of aliasing imports. Instead, when you call a class from a package, you can give the full package name before the class name. For example, when calling `FooClass.methodName()` after importing a different copy of `FooClass`, `com.company.utils.FooClass.methodName()` specifies the `FooClass` in the `com.company.utils` package. This allows you to reference multiple copies of `FooClass`.- Full example:
- ```java
- import com.company.special.FooClass;
- class BarClass {
- public void exampleMethod(){
- //Call method1 from FooClass that we imported above
- int i = FooClass.method1();
- //Call a class named FooClass that is in a different package
- String str = com.company.utils.FooClass.methodName();
- }
- }
- ```
- Java 8 does not have a method of aliasing, or 're-naming', imports. When you call a class from a package, you can give the full package name before the class name.
- For example, when calling `FooClass.methodName()` after importing a different copy of `FooClass`, `com.company.utils.FooClass.methodName()` specifies the `FooClass` in the `com.company.utils` package. This allows you to reference multiple copies of `FooClass`.
- Full example:
- ```java
- import com.company.special.FooClass;
- class BarClass {
- public void exampleMethod(){
- //Call method1 from FooClass that we imported above
- int i = FooClass.method1();
- //Call a class named FooClass that is in a different package
- String str = com.company.utils.FooClass.methodName();
- }
- }
- ```
#1: Initial revision
Java 8 does not have a method of aliasing imports. Instead, when you call a class from a package, you can give the full package name before the class name. For example, when calling `FooClass.methodName()` after importing a different copy of `FooClass`, `com.company.utils.FooClass.methodName()` specifies the `FooClass` in the `com.company.utils` package. This allows you to reference multiple copies of `FooClass`. Full example: ```java import com.company.special.FooClass; class BarClass { public void exampleMethod(){ //Call method1 from FooClass that we imported above int i = FooClass.method1(); //Call a class named FooClass that is in a different package String str = com.company.utils.FooClass.methodName(); } } ```