Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Imports
Java Basics Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Basics Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.
-->
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
Imports are the next building block of Java programming that is essential to master in order to write advanced programs. Java gives you the advantage of pre-built classes and methods that can perform many functions such as reading a content file or comparing different dates. The classes are organized into packages that must be imported into your project in order to be used. Let's look at two ways to import a class.
Below is an example of a method you can use to import a class called ArrayLists.
ImportExample.java
package exlcode;
// imports
import java.util.ArrayList;
public class ImportExample {
public static void main(String[] args) {
// This will be explained in Unit 7
ArrayList exampleVariable = new ArrayList();
exampleVariable.add("Hello");
exampleVariable.add("World!");
// This prints exampleVariable
System.out.println(exampleVariable);
}
}
Take note of the fact that the IMPORT statement is located at the very beginning of this example. In words, it says: "in the package java.util there is a class called ArrayList and we want to use it in our program".
A second method to import classes is using the '*' symbol. For example, to import ALL the classes in the java.util package, we would write import java.util.*
Notice that the java.lang package is auto-imported for you so that later in the course, when you begin using String and Math classes, you do not need to import them.