Import statements let you use code from other places easily. They save time and keep your code clean.
Import statement usage in Java
import packageName.ClassName; // or to import all classes in a package import packageName.*;
The import statement must be placed at the top of your Java file, before any class definitions.
Using * imports all classes in a package but does not import sub-packages.
import java.util.Scanner;import java.util.*;import static java.lang.Math.PI;
This program uses the Scanner class to read user input. The import statement lets us use Scanner without writing java.util.Scanner every time.
import java.util.Scanner; public class ImportExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name + "!"); scanner.close(); } }
Import statements do not import code automatically; they just let you use names without full paths.
If you forget to import a class, Java will show an error saying it cannot find the symbol.
You can also use fully qualified names without import, but it makes code longer and harder to read.
Import statements help you use classes from other packages easily.
Place import statements at the top of your Java file.
You can import single classes or all classes from a package using *.
Practice
import statement in Java?Solution
Step 1: Understand the role of import statements
Import statements let you use classes from other packages without writing their full package names every time.Step 2: Differentiate from other Java keywords
Import does not declare packages, create classes, or start methods; it only helps with class usage.Final Answer:
To allow the use of classes from other packages without full package names -> Option AQuick Check:
Import = Use classes easily [OK]
- Confusing import with package declaration
- Thinking import creates classes
- Assuming import runs code
java.util?Solution
Step 1: Recall the syntax for importing all classes
To import all classes from a package, use the asterisk (*) after the package name with a semicolon.Step 2: Check each option's syntax
Onlyimport java.util.*;is correct. Others miss the asterisk or use wrong keywords.Final Answer:
import java.util.*; -> Option DQuick Check:
Use * to import all classes [OK]
- Omitting the asterisk (*) when importing all classes
- Missing semicolon at the end
- Using incorrect keywords like 'all' or 'from'
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
System.out.println(list.get(1));
}
}Solution
Step 1: Understand the import and list usage
The code imports ArrayList correctly and creates a list with "Hello" at index 0 and "World" at index 1.Step 2: Check the printed element
list.get(1) accesses the second element, which is "World".Final Answer:
World -> Option CQuick Check:
list.get(1) = World [OK]
- Confusing index 0 and 1
- Assuming import causes error
- Thinking output is class name
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<String> items = new ArrayList<>();
}
}Solution
Step 1: Check import statements
The code imports List but does not import ArrayList, which is used in the code.Step 2: Identify missing import causing error
ArrayList must be imported from java.util to be used; missing import causes compilation error.Final Answer:
ArrayList is not imported -> Option BQuick Check:
Missing import for ArrayList causes error [OK]
- Forgetting semicolon after import
- Assuming List needs initialization differently
- Ignoring missing imports for used classes
java.util.Date and java.sql.Date in the same Java file. How should you import them to avoid confusion?Solution
Step 1: Understand class name conflict
Both packages have a class named Date, so importing both directly causes ambiguity.Step 2: Use import for one and full name for the other
Import one Date class and use the full package name for the other to avoid confusion.Final Answer:
import java.util.Date; // Use fully qualified name for java.sql.Date in code -> Option AQuick Check:
Import one Date, fully qualify the other [OK]
- Importing both classes directly causing ambiguity
- Using wildcard imports causing confusion
- Not using fully qualified names when needed
Ready to prove it?
Pass the test and this pattern moves unknown
