Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard imports usage in Java and Scala

Recently I have heard statements like "you should never use wildcard imports" too often. So I want to ask community about this. Should wildcard imports really never ever be used in Java production code, no matter what? Are there exceptions to this rule? I interested in your personal experience and opinion. Do you use them in your production code and would you recommend it to others? How do you use them - can you recommend the best way to make it.

It also interesting to look at it from Scala perspective. Is the same applies to Scala? Or wildcard imports in Scala should be only used in presentation slides and SO answers?

If you will look at scalaz page, for example, they recommend usage of wildcard imports like:

import scalaz._
import Scalaz._   

I think it also important to consider implicit conversions that are normally imported with wildcards.

like image 723
tenshi Avatar asked Mar 15 '11 11:03

tenshi


People also ask

Should I use wildcard imports in Java?

In fact, the book recommends using wildcard imports when using multiple classes from the same source. In other words, when we import two or more classes imported from a package, it's better to import the whole package.

Should we use * in import Java?

Java developers use the wildcard (*) in import statements to import all the classes in a particular package. But in code reviews, most of you may have asked to remove those wildcard imports and add the actual class name.

What is import * in Java?

import is a Java keyword. It declares a Java class to use in the code below the import statement. Once a Java class is declared, then the class name can be used in the code without specifying the package the class belongs to. Use the '*' character to declare all the classes belonging to the package.

What are specific and wildcard imports?

Specific imports are hard dependencies, whereas wildcard imports are not. If you specifically import a class, then that class must exist. But if you import a package with a wildcard, no particular classes need to exist. The import statement simply adds the package to the search path when hunting for names.


2 Answers

Well, by specifiying full classnames you remove ambiguity. So, when you explicitly state which class to import it's a lot easier to understand the intention of the code. Java 1.2 also comes to mind:

import java.util.*;
import java.awt.*;

...
List blah;

This worked fine in Java 1.1. However, in Java 1.2 a List interface was added to java.util, and the code that used to be fine didn't work anymore. Lots of developers cried.

like image 164
Andreas Johansson Avatar answered Sep 25 '22 21:09

Andreas Johansson


In Scala, wildcard imports are a must, since many libraries expect their implicit conversions to be in scope, but they're not always conveniently named. So,

import collection.JavaConversions._

is a great idea, whereas

import collection.JavaConversions.{asJavaConcurrentMap,enumerationAsScalaIterator,...}

is incredibly awkward. Better yet, in Scala you can put your imports in any scope:

package mypackage {
  class MyClass {
    def myGraphicalWidgetHandler {
      import java.awt._
      ...
    }
    ...
  }
  ...
}

which really helps keep the namespace clutter down throughout the whole file. And you can selectively rename parts of the import that you know will conflict:

import java.awt.{List => AwtList, _}

In contrast, in Java, you're restricted to global scope for imports, and you can't rename them; you also don't have implicit conversions, so it's okay to only pull in those things that you're looking for. On the other hand, you have powerful IDE support that will help find the class that you're looking for and import just it for you. So for Java, there's a reasonable argument to be made that you should let your IDE pull in just what you need rather than you deciding to grab everything. Personally, I still find this too awkward and just use wildcard imports most of the time.

like image 33
Rex Kerr Avatar answered Sep 24 '22 21:09

Rex Kerr