The import Statement in Java

Java’s import statement allows you to access classes, interfaces and methods located in a different package. By using an import statement you tell the Java runtime to go and use (but not fetch) a certain artefact lying in a different package. The import statement is also an indication to the Java compiler where to look for and use additional class and interface definitions. Note that import is a syntactic sugar for bringing down the noisiness of the code. It is possible to write fully functional Java code without import. We will be covering static import in the next article.

What and How of import

If your class, say X, needs to access another class Y ( may be for creating objects, invoking methods, using static methods) and Y happens to be located in another package then you need to explicitly tell Java where to find Y by fully qualifying with it with Y‘s package name.

An this is how the class X can import and use Y

All the classes in the same package are directly accessible. You don’t need to import them.

Classes located in parent packages are not imported by default. You need to import these although they are in the same package tree as your class.

3 Ways of importing

You can use one of the following ways to import –

importing Everything From a package

This form import uses * to make everything located in a package accessible.

This may make a lot of unwanted classes accessible to your code. This creates an unwanted clutter and may sometimes lead to name conflicts and collisions (generally at compile time).

It is extremely unprofessional and careless to use this method. It has been listed here just to make sure it is out there and you SHOULD NOT USE this form.

Let me repeat. It’s worth it.

Thou shalt not import *

Some developers wrongly think that importing * brings a lot of code in their code base. Nothing else can be further from truth. (See the section below)

This form is HIGHLY discouraged because it is a maintenance nightmare. But it does not increase your code base or incur additional runtime performance penalty.

importing only What is Required

Importing only what you need is the correct form of import. As  was shown in the first example at the beginning of this article, this type of import statement manages access to only a single class.

The above form clearly tells the reader what you want to import. This is a major boost to maintainability of your code. No Sherlock Holmes style art of deduction required.

The only downside of this method is that you may have more import statements at the top. That’s ok, because on any day being verbose is better than failing in unexplained, mysterious ways.

Use Full Names

This is actually a way of avoiding rather than using import statement. An import is just a way to reduce clutter in your code. If you don’t want to import you can use the fully qualified name of a class in your code.

What are Default imports?

There are three packages that are imported by default in your Java program. You don’t need to (but can) import them. These are –

  1. java.lang package. The Java runtime loads the contents of this package and make them available to you. This is the reason that when writing System.out.println you don’t need to import System
  2. Your current package. All classes and interfaces in your current package are made available implicitly.
  3. The default no-named package. Whenever you declare a class without a package name declaration, Java puts it in a virtual default package. As far as the Java runtime is concerned all classes without package name declaration form a separate default package. This package is available to your application without import to other classes.

Is Java import similar to #include of C/C++ or #import of Objective-C?

ABSOLUTELY NO.

Java’s import statement is like giving someone an address card. It tells the compiler and runtime what code (read data types) to make use of. Both compiler and runtime make use of information provided by import.

C/C++ use #include directives. This is a preprocessor statement. It is not a runtime phenomenon. #include is like asking to find and bring the residents of an address at your location. The preprocessor includes the source code of the included file in the current file. Therefore, import affects the size of your code but import does not.

#import of Objective-C is a  variant of #include . It does not work like Java’s import. It avoids repeated inclusion of header files. Therefore, it is more like require_once or include_once of PHP.

Leave a comment

Your email address will not be published.