If this is possible, what part of java compiler need to be re-engineered to has an "import as" statement, so codes can look like this way:
import java.util.Date;
import mypackage.Date as MyDate;
//...
javaDate = new Date();
myDate = new MyDate();
Even more, what need to get this kind of syntax:
import java.util.Date;
import path.to.mypackage as MP;
//...
javaDate2 = new Date();
myDate2 = new MP.Date();
And what trouble it will cause for existing codes?
To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.
In Java, import is simply used by the compiler to let you name your classes by their unqualified name, let's say String instead of java. lang. String . You don't really need to import java.
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for: Preventing naming conflicts. For example there can be two classes with name Employee in two packages, college.
This obviously requires changes to the Java compiler. If you're really interested in doing something like this, then look at the OpenJDK project, where you will find the source code for Oracle's Java compiler and runtime environment.
If you want to propose this as a new feature for a future version of Java, then you'd have to go through the Java Community Process.
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