Java Imports

Anna Scott
5 min readOct 18, 2021

There are over 5000 built-in classes in JAVA, and it is hard to say how many classes that were created by developers like you and me. Java’s “import” declarations (sometimes called statements) allow the use of all these classes. To do that, we have to follow the JAVA keyword “import” with the full path to the class that you want to use. In the following example to use the ArrayList class we need to import it first.

package imports;

import java.util.ArrayList;

public class LearningImports {
public static void main(String... input) {
ArrayList<String> arr = new ArrayList<>();
}
}

The import declaration comes after the package statement and before class definition. Just like the package statement the import declaration(could be more than one) is not required, however if you have to use you will need to write out the fully qualified classname (java.util.ArrayList).

public class LearningImports {
public static void main(String... input) {
java.util.ArrayList<String> arr = new java.util.ArrayList<>();
}
}

You can see how the use of import statements save us a lot of typing.

If you are using a number of classes from the same package, instead of having imports for each of them like here:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class LearningImports {
public static void main(String... input) {
ArrayList<String> arr = new ArrayList<>();
HashMap<String,String> hm = new HashMap<>();
HashSet<String> hs = new HashSet<>();
}
}

You can use a wildcard:

import java.util.*;

public class LearningImports {
public static void main(String... input) {
ArrayList<String> arr = new ArrayList<>();
HashMap<String,String> hm = new HashMap<>();
HashSet<String> hs = new HashSet<>();
}
}

The “*” after java.util. is called the wildcard, it means all the classes in the java.util package are available during compile time, this means that the use of wildcard does not have an effect on performance, however it might affect the compilation time:

Using the wildcard shortens the import list, however there is an opinion that imports with wildcards makes the program that has them hard to read.

Since JAVA is useless without functionality in java.lang package, so it is imported implicitly. We don’t have to, however can write out:

import java.lang.*;

There is no need to import classes that are in the same package as the class that is going to use them:

public class LearningImports {
public static void main(String... input) {
NoNeedToImport obj = new NoNeedToImport();
System.out.println("Hi " + obj.name);
}
}

class NoNeedToImport{
String name = "Anna";
}

result of above program:

Hi Anna

We can have classes with the same name in the different packages. This might create some naming conflicts in our code. Take a look at the following packages and their classes:

package imports.carnivour;

public class Food {
public boolean meat = true;
}
package imports.herbivore;

public class Food {
public boolean meat = false;
}
package imports.omnivore;

public class Food {
public boolean meat = true;
}

You can see that we have three packages with the classes that have the same name Food. Lets create another package with the class that would like to use class Food. If we try to import all the three packages with the wild card the compiler would complain:

package imports.zoo;

import imports.carnivour.*;
import imports.herbivore.*;
import imports.omnivore.*;

public class ZooKeeper {
Food food;
}

reference to Food is ambiguous.

Lets replace one of the imports with explicit class import:

package imports.zoo;

import imports.carnivour.*;
import imports.herbivore.Food;
import imports.omnivore.*;

public class ZooKeeper {
Food food;
}

All is good here. Explicit import of the class takes precedence over any wildcard import.

What if we try to import all the classes implicitly:

package imports.zoo;

import imports.carnivour.Food;
import imports.herbivore.Food;
import imports.omnivore.Food;

public class ZooKeeper {
Food food;
}

JAVA calls us out on it:

reference to Food is ambiguous.

How do we handle the situation where we have to classes with the same name from the different packages. Check out the following program:

package imports.zoo;

import imports.omnivore.Food;

public class ZooKeeper {
imports.carnivour.Food foodForCarnivour;
imports.herbivore.Food foodForHerbivore;
Food foodForOmnivore;

public void eat() {
foodForCarnivour = new imports.carnivour.Food();
System.out.println("Carnivour eats meat: " + foodForCarnivour.meat);
foodForHerbivore = new imports.herbivore.Food();
System.out.println("Herbivore eats meat: " + foodForHerbivore.meat);
foodForOmnivore = new Food();
System.out.println("Omnivore eats meat: " + foodForOmnivore.meat);
}

public static void main(String... input) {
ZooKeeper me = new ZooKeeper();
me.eat();
}
}

We have three classes that have the same name, we import two of them explicitly, and let the last one be imported with the wildcard.

There is another type of import — static import. Static imports are used for importing static members of classes.

Let’s look at the Java’s Math class which has a number of static methods for performing basic numeric operations:

We specifically take a look at max() method that returns greater of two values:

public class MathPackage {
public static void main(String... input) {

int val1 = 10;
int val2 = 100;

int maxValue = Math.max(val1, val2);

System.out.println("Greater value is " + maxValue);
}
}

The output:

Greater value is 100

In the above example, we are calling our static method max() using class name.

We can use the static import to import our method, please take a look:

import static java.lang.Math.max;

public class MathPackage {
public static void main(String... input) {

int val1 = 10;
int val2 = 100;

int maxValue = max(val1, val2);

System.out.println("Greater value is " + maxValue);
}

Static imports allow us to use static methods of classes without writing class’s name.

If you do a static import and don’t import the class where static member comes from, our code is not going to compile:

import static java.util.Arrays.asList;

public class ErrorStaticImport {

public static void main(String... input) {
int[] arr = {1,2,3};
Arrays.asList("one");
}
}

The above code generates:

java: cannot find symbol
symbol: variable Arrays
location: class StaticImport.ErrorStaticImport

In the code snippet above we statically imported method asList, however we did not provided import for the class Arrays where the method located.

Imports make our life is easier, because we don’t have to specify where each class comes from every time we use it, or we don’t have to specify where the static members come from every time we use them.

Happy coding my friends!

All the code can be found here:

--

--