O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required