Overview

In Java, the import statement is used to bring certain classes or the entire packages, into visibility. As soon as we use the import statement, a class can be referred to, directly by using only its name. Let's take a look at this example.

Example 1 - Using import statement

In this example, we're trying to create an object named ArrayList inside the main method within the class Test, and we save it as Test.java. After examining the code, you may wonder if this Java file will compile successfully. The answer is no. This is because the compiler doesn't recognize ArrayList, resulting in an error.

public class Test{
  public static void main (String[] args){
    ArrayList myObj = new ArrayList();
  }
}

To solve this error, we have 2 solutions. Before that, we need to know which package contain ArrayList class. In this example, ArrayList class is taken from java.util package.

The first solution is by using fully qualified name, where in this case, we just need to add java.util. in front of ArrayList.

public class Test {
    public static void main(String[] args) {
        ArrayList myObj = new java.util.ArrayList();
    }
}

The term "fully qualified name" refers to using the complete package name, such as java.util.ArrayList. While this approach is technically correct, it may reduce code readability when we need to use the ArrayList class multiple times in our program. In such cases, a more practical solution is to use the import statement.

The second solution is by using the import statement. In the provided example, instead of repeatedly using java.util., we can enhance code readability by adding the import statement import java.util.ArrayList before the class definition. This way, the code remains concise, and it compiles successfully.

import java.util.ArrayList;
public class Test {
    public static void main(String[] args) {
        ArrayList myObj = new ArrayList();
    }
}

Java Import Statement

In Java programming, import statements play a crucial role in making classes and packages accessible. There are two primary types of import statements: explicit import and implicit import.

Explicit Import Statement

import java.util.ArrayList;

This line explicitly specifies the ArrayList class, making it clear which class is being referred to.

Implicit Import Statement

import java.util.*;

This statement signifies that all classes within the java.util package are available for use. While it is common for programmers to opt for implicit import statements, it is recommended to lean towards explicit imports. The primary reason for this recommendation is to avoid potential naming conflicts in our programs.

Understanding Naming Conflicts

Naming conflicts can arise when using implicit import statements. To illustrate, consider the scenario where we have two implicit import statements:

import com.maybank.*;
import com.junebank.*;

Both packages (com.maybank and com.junebank) contain classes named Account and Loan. Now, if we create two new objects in our program:

Account A = new Account();
Loan L = new Loan();

The issue here is that both Account and Loan classes exist in both packages, leading to confusion for the compiler. To mitigate this, we can opt for explicit imports:

import com.maybank.Loan;
import com.junebank.Account;

By explicitly stating the package, we eliminate ambiguity and prevent naming conflicts.

In conclusion, while implicit import statements are not inherently wrong, the best practice is to use explicit import statements. This ensures code clarity, reduces the risk of naming conflicts, and contributes to a more maintainable and readable Java program.

Exploring Import Statements in Java: Key Highlights

In this section, we will delve into some essential rules and considerations related to the Java import statement.

No Import Statement Required for java.lang Classes

// Test.java
  public class Test {
    public static void main(String[] args) {
      String str = "Hello";
      Thread thread = new Thread();
      Exception exception = new Exception();
      StringBuffer stringBuffer = new StringBuffer();
      // No explicit import statement needed for classes in java.lang
    }
  }

In this code, classes such as String, Thread, Exception, and StringBuffer from the java.lang package are used without requiring import statements. The code will compile successfully.

No Import Statement Required for Classes in Current Working Directory

// Student.java
  public class Student {
    // Class definition
  }

  // Test.java
  public class Test {
    public static void main(String[] args) {
      Student student = new Student();
      // No explicit import statement needed for Student class in the current directory
    }
  }

In this example, the Student class is used in the Test class without an import statement because both classes reside in the same directory. The code compiles successfully.

In summary, there are two conditions where an import statement is not required: when using classes in the java.lang package and when the class is present in the current working directory.

Importing Classes in Packages and Sublevel Packages

// Importing class in package and sublevel package
  import java.util.regex.*;

  // Rest of the code

Here, the correct import statement is import java.util.regex.*. This is because importing a package (java.util) includes all classes and interfaces within that package but not its subpackages.

Remember, when importing a class from a package, all classes in that package become available, excluding those in subpackages. Similarly, when importing a class from a subpackage, all classes in that specific subpackage become available, excluding those in the main package.

End Of Article

End Of Article