
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between import and static import statements in Java?
The import statement is used to bring certain classes and interfaces from other packages into our Java program, so we can access them without using their fully qualified names. We can use the short name instead.
Java also supports static import statements, which were introduced in Java 5. It helps in accessing static members such as methods and constants. In this article, we are going to learn the difference between import and static import statements in Java.
The import Statement in Java
To access a class or method from another package, we need to either use the fully qualified name or use the import statement. However, the class or method must be accessible, and its accessibility is determined by the Java access modifiers.
For example, private members are only accessible within the same class, so they cannot be accessed even with the fully qualified name or the import statement.
Important! the java.lang package is automatically imported into our code by Java.
Example
In this example, we will show the use of the import statement in Java:
import java.util.Vector; public class ImportDemo { public ImportDemo() { //Imported using keyword Vector<String> v = new Vector<>(); v.add("Tutorials"); v.add("Point"); v.add("India"); System.out.println("Vector values are: "+ v); //Package not imported java.util.ArrayList<String> list = new java.util.ArrayList<>(); list.add("Tutorix"); list.add("India"); System.out.println("Array List values are: "+ list); } public static void main(String arg[]) { new ImportDemo(); } }
Output of the above code:
Vector values are: [Tutorials, Point, India] Array List values are: [Tutorix, India]
The static import Statement
The static import allows us to import static members so that they can be used without specifying the class name. Its declaration has two forms: one that imports a particular static member, which is known as a single static import, and one that imports all static members of a class, which is known as a static import on demand.
Example
In this example, we are importing System using the static keyword:
//Using Static Import import static java.lang.System.*; public class StaticImportDemo { public static void main(String args[]) { //System.out is not used as it is imported using the keyword static out.println("Welcome to Tutorials Point"); } }
The above code will produce the following output:
Welcome to Tutorials Point
The import VS static import statements in Java
Now, let's discuss the difference between import and static import statements:
import statement |
static import statement |
---|---|
The import statement imports classes and interfaces from another package. |
The static import statement imports static members of a class. |
Once imported, short name of the classes can be used. |
Once imported, static members can be used without the class name prefix. |
It cannot access private or default members from other packages. |
It cannot access private static members. |
Example: you can import classes like java.util.*, java.io.*, etc. |
Example: you can import static constants or methods like System.out, Math.PI, etc. |