Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 24, 2000

Doing an [import java.package.*]

This tip is probably cosmetic, but it could result in great convenience. Doing a [import java.package.*] would allow usage of [public] classes in the aforementioned package without the usual required verbosity. This means that [java.package.class1] and [class1] would mean the same thing (and be legal as well).

But one thing about it is that the classes in subpackages are not imported with those in the package. Meaning that [java.package.subpackage.class2] cannot be referenced legally as [class2]. You would have to do a [import java.package.subpackage.*]

For those who are not greedy, you can do a simple [import java.package.class3] and reference it in your code as [class3] this may come in useful when [java.package1] and [java.package2] both contain a class called [UnfortunatelyCommonClass] and you want access to some classes in each.

An error will arise if wildcards (*) are used in importing and classes in different packages imported have the same name. But if classes are imported without wildcards like so:

 
import java.package1.UnfortunatelyCommonClass;
import java.package2.*
[UnfortunatelyCommonClass] references the one in [java.package1]. But if this is done:
 
import java.package1.*
import java.package2.*
Using [UnfortunatelyCommonClass] would return an error. Happy importing.
Jeremy Chen
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date