Using External Code in Your Programs: import

Almost every time you write a new class file, you will want to give that class the powers provided by some existing external file that lives in another package. You can do that in one of two ways:

  1. Specify the fully qualified class name every time you reference the class.

  2. Import the class or the package containing the class, and then simply reference the class using its name. You do this using the keyword import.

If you have used Microsoft .NET, you should be familiar with this concept. In C#, you might write the following:

using System;
class MyClass {
  public static void Main() {
    Console.WriteLine("smeagol was here");
    }
}

That's how you specify that you are going to use one or more of the classes ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.