Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What need to be done to implement "import ... as ..." in java

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?

like image 573
Andrew_1510 Avatar asked Mar 08 '12 05:03

Andrew_1510


People also ask

How do you get imports in Java?

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.

How does import in Java work?

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.

What is implemented in Java as a package?

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.


1 Answers

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.

like image 83
Jesper Avatar answered Oct 05 '22 17:10

Jesper