Java Tutorial Java Advanced Java Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java import Keyword



The Java import keyword is used to import a package, class or interface to the current source code. The import statements must be placed on top of the source file, only after the package statement.

To import all classes/interfaces from a package the * wildcard character can be used. See the syntax below:

Syntax

//imports all classes/interfaces 
//from java.awt package
import java.awt.*;

//imports List interface only
//from java.util package
import java.util.List;

//imports File class only
//from java.io package
import java.io.File;

Example:

In the example below, ArrayList class is imported from java.util() package to use in the current source code.

import java.util.ArrayList;

public class MyClass {
  public static void main(String[] args) {
    //creating a ArrayList
    ArrayList<Integer> MyList = new ArrayList<Integer>();

    //populating ArrayList using add() method
    MyList.add(10);
    MyList.add(20);
    MyList.add(30);
    MyList.add(40);
    MyList.add(50);

    //printing ArrayList
    System.out.println("MyList contains: " + MyList);   
  }
}

The output of the above code will be:

MyList contains: [10, 20, 30, 40, 50]

❮ Java Keywords

5