Using Imports in Java and importing packages

What does the import statement in java do?

In an earlier tutorial we saw an introduction to java. In this tutorial, we will look at using imports in java. To state it plainly – import statements allows us to use the name of the class in our code instead of its fully qualified name.

What is a fully qualified name in java?

Lets say you have the following class structure.
using imports in java

We want to use the Addition class in our Imports class. If we use it directly, the compiler would give us an error
Using Imports in java
This is because the compiler does not know where the Addition class is. We therefore give the complete path of the Addition class.Using Fully Qualified Name in Java
This works well, however imagine doing this for all classes that you use in your file. Its soon going to be unmanageable. That is where the import statement comes in.

Using import statement instead of fully qualified name

We add this statement before the class definition:

This tells the compiler that I am going to use the Addition class from the com.studytrails.core.packages package, so whenever you see Addition, it means

Using imports in java

Using imports in java for packages and classes with the same name

So far, we have seen how to import a class, but what happens if you have two classes with the same name (in different packages).

Importing two classes in different packages but with the same name

Let’s say you have another Addition class in a different package. This class might come from a different library that is capable of adding really huge numbers.
java import same name classes
How would you handle this? Can you import both classes? If you did import both classes, how would the compiler know which class you are referring to in your code? If I write

the compiler has no way of knowing which Addition class you are referring to. Therefore, if you have two classes with the same name then you can import only one and you will have to use the fully qualified name for the other.

Importing an entire package

If you have multiple classes from the same package that you are planning to use in your code then it would make more sense to import the complete folder instead of all classes individually.
java import folder
If we want to use all the above four classes in our code, then we use a wildcard to import all classes from that folder;

using imports in java for folder
There is an interesting case, though. What if you have the Addition class both in packages folder and in the bignumbers folder and you import the entire packages folder?
using imports in java
Using imports in java
In this case, the compiler uses the Addition class from the bignumbers packages since that has been directly imported. If you now wanted to use the Addition class from the packages folder you will have to use a fully qualified name. Also note that importing the folder imports all the classes from the folder but does not import the sub folders.

Static Imports in java

To use a static method from a class, you need to qualify the method name with the class name.

We use the divide method from the Division class. To use divide method directly without using Division.divide we need to do a static import

static imports allow us to use the static methods directly without the class. You can static import all methods in a class as well:

This finishes our discussion on using imports in java. In the next tutorial, we will look at the access modifiers in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *