Java class and source file


🙋 Need help? Ask an expert now!

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.
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();
    // This prints 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.

Edit Me on GitHub!

Application Question

Which of the following is a correct declaration of an imported class?