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:

import com.studytrails.java.core.packages.Addition;

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 com.studytrails.java.core.packages.Addition

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

Addition addition = new Addition();

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;

import com.studytrails.java.core.packages.*;

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.

package com.studytrails.java.core.packages;
public class Division {
	public static double divide (double a, double b) {
		return a/b;
	}
}
package com.studytrails.java.core;

import com.studytrails.java.core.packages.Division;

public class Imports {
	public static void main(String[] args) {
         Division.divide(2, 2);
	}
}

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

package com.studytrails.java.core;
import static com.studytrails.java.core.packages.Division.divide;
public class Imports {
	public static void main(String[] args) {
         divide(2, 2);
	}
}

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

package com.studytrails.java.core;

import static com.studytrails.java.core.packages.Division.*;

public class Imports {
	public static void main(String[] args) {
          divide(2, 3);
	}
}

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

1 thought on “Using Imports in Java and importing packages”

  1. Sorry, kinda confusing…. so if we have TWO files… one with the main program in it, and the other with a bunch of classes that the main program uses; they are in same directory, how would you import into main program? And what would you write into the class file?

    Reply

Leave a Comment