Import Statement
/*
class Test {
public static void main(String... args) {
ArrayList arr = new ArrayList();
}
}
Compile Time Error:
cannot find symbol: class ArrayList
location: class Test
ArrayList arr = new ArrayList();
^
*/
- We can solve this problem by using a fully qualified name: java.util.ArrayList arr = new java.util.ArrayList();
- The problem with using a fully qualified name every time is it increases the length of the code and reduces readability.
- We can solve this problem by using import statements.
- Whenever we are writing import statements, it is not required to use fully qualified names; we can use short name directly.
Example:
import java.util.ArrayList;
class Test {
public static void main(String... args) {
ArrayList arr = new ArrayList();
}
}
Hence, import statement as typing shortcut.
Case 1: Types of Import Statements
There are two types of class import statements: Explicit and Implicit.
Explicit Class Import:
- It is highly recommended to use explicit class import because it improves the readability of code.
- Best suitable for high-tech cities where readability is important.
Implicit Class Import:
- Not recommended to use because it reduces the readability of the code.
- Best suitable where typing is important.
Case 2: Which of the Following Import Statements is Meaningful
import java.util.ArrayList;import java.util.ArrayList.*;import java.util.*;import java.util;
Case 3: Consider the Following Code
class myObject extends java.rmi.UnicastRemoteObject
The code compiles fine even though we are not writing an import statement because we used a fully qualified name.
class myObj extends java.rmi.UnicastRemoteObject {
}
Note: Whenever we are using the fully qualified name, it is not required to write an import statement. Similarly, whenever we are writing an import statement, it is not required to use the fully qualified name.
Case 4: Ambiguity Problem
Note: Even in the case of List, we may get the same ambiguity problem because it is available in both util and awt packages.
import java.util.*;
import java.sql.*;
class Test {
public static void main(String[] args) {
Date date = new Date();
}
}
Compile time error: reference to Date is ambiguous
Case 5: Precedence Order
While resolving the class name, the compiler will always give precedence in the following order:
- Explicit class import
- Class present in the current working directory (default package)
- Implicit class import
Example:
import java.util.Date;
import java.sql.*;
class Test {
public static void main(String... args) {
Date d = new Date();
System.out.print(d.getClass().getName());
}
}
Output: java.util.Date
In the above example, the util package got considered.
Case 6: Importing Subpackages
Whenever importing a Java package, all classes and interfaces present in that package are by default available, but not subpackage classes. If we want to use subpackage classes, we must write import statements up to the subpackage level.
Eg:
To use Pattern class in our program, which import statement is required:
import java.*;import java.util.*;import java.util.regex.*;(correct)- No import required
Case 7: Default Packages
All classes and interfaces present in the following packages are by default available to every Java program; hence, we are not required to write import statements:
- java.lang package
- Default package (current working directory)
Case 8: Import Statement and Performance
Import statements are totally compile-time related concepts. If more imports are used, it increases compile time, but there is no effect on execution time (runtime).
Case 9: Difference Between C Language #include and Java Import Statement
In C language, #include loads all input/output headers at the beginning at translate time; hence, it is static include.
But in Java, the import statement does not load .class files at the beginning. Whenever we use a particular class, only the corresponding .class file will be loaded. This is like dynamic include or load on demand or load on the fly.
0 Comments
Hello