Loading...

Java import

Keep class Bell in folder kit with package kit; and ring() that prints ding. In Porch.java beside that folder, write import kit.Bell; near the top, then call Bell.ring(); from main with the short name. Compile from the parent folder and run java Porch. The screen should show one line: ding. The import line lets you drop kit. at the call. That short-name call after import is what this page teaches.

A line that opens the short name

On the packages page you typed kit.Bell.ring(); every time. That full name still works. An import line tells Java which package class you mean by the short word Bell. After import kit.Bell;, the call can be Bell.ring();. The Bell file itself does not change its package kit; line.
Keep the same kit/Bell.java you used before, or recreate it. First line package kit;. Public class Bell. Method:
public static void ring() { System.out.println("ding"); }
There is no main in Bell.java. Save before you compile. Do not put the import line inside Bell for this lesson — import belongs in the file that wants the short name, here Porch.

Bell in package kit

java
package kit;

public class Bell {
    public static void ring() {
        System.out.println("ding");
    }
}

Porch imports then calls Bell.ring

Create Porch.java in the parent folder — the same level as kit, not inside kit. Put import kit.Bell; after any package line of Porch (Porch has none on this page) and before public class Porch. Inside main, write Bell.ring();. Do not write kit.Bell.ring(); as the required path on this demo — the import is the lesson.
Keep Porch as the class that holds main. If a leftover Lobby.java from the packages page still calls only kit.Bell.ring();, you may leave that file aside. For this lesson run java Porch after you compile from the parent folder.

Porch with import

java
import kit.Bell;

public class Porch {
    public static void main(String[] args) {
        Bell.ring();
    }
}

From the parent folder that holds Porch.java and the kit folder, run javac kit/Bell.java Porch.java. On Windows you can point at kit\Bell.java the same way. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Porch — not java Porch.java, and not java kit.Bell. Leave the .java ending off the run command. Start the class that holds main.
The program should print one line and stop: ding. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call and keep import kit.Bell; plus Bell.ring(); in Porch.
Walk the run once. Java starts at Porch.main. The import has already named which Bell you mean. The call Bell.ring() runs the static method in package kit. The body prints ding. The screen shows ding. Nothing else should print. If you see 9 or ok, you are still running an older Pier or Yard program from a previous page.
If you write Bell.ring(); with no import and no kit. prefix, javac cannot find the class on this page. Add import kit.Bell; above the class, or use the full name. If you place import after public class Porch, javac refuses — imports belong before the class declaration. Spelling matters too: import kit.Bell; must match the package folder and the class name character for character.

Full name still works without import

You can still call kit.Bell.ring(); with no import line. That is the packages-page shape. A second Porch demo keeps that path so you see both styles.

Porch full name no import

java
public class Porch {
    public static void main(String[] args) {
        kit.Bell.ring();
    }
}

Compile and run that shape the same way: javac kit/Bell.java Porch.java, then java Porch. The screen still shows ding. The import page’s preferred teaching file is the one with import kit.Bell; and Bell.ring();. Use the full-name file only to prove the old call still compiles. Do not mix both calls as the one file you save for the quiz path — pick the import shape as the lesson file.
If the screen shows a leftover 9 or ok, an older Pier or Yard program is still running. Switch to java Porch. If javac says package kit does not exist, check that Bell.java still sits under folder kit with package kit; as its first line.

Compared with packages

On the packages page the only call was kit.Bell.ring();. This page adds import kit.Bell; so the short name works. A leftover Lobby that never imports is fine as history, not as this lesson’s required file. For this page keep import kit.Bell; and Bell.ring(); in Porch.
You do not need import inside the Bell file. Bell already knows its own package. Import is for the caller outside the package that wants a shorter name. Think of import as a local nickname line for this source file only — other files do not inherit that nickname unless they write their own import too.
When you later open several classes from the same package, you may write several import lines, one per class. This page keeps a single import so the short call stays easy to see.

What this leaves for ArrayList

This page finishes the Classes module’s package story with import. The next page opens Collections with ArrayList. Stay with import and the short call here. Do not invent wildcard import kit.*; as a requirement on this page; one exact import is enough.
When old Lobby.java files still sit in the parent folder, leave them alone or delete them only if they confuse your compile command. Run java Porch after you compile the import pair from this lesson.

Before ArrayList

You can keep Bell under kit with package kit;, write import kit.Bell; in Porch, and call Bell.ring();. Compile from the parent folder and run java Porch. The screen shows ding. The import opened the short name. The next page is ArrayList, where you store a growing list of values in one object.
Keep the import above the class. Keep the short call Bell.ring(); in the teaching file. Keep compiling from the parent folder. Those three habits are the checkpoint for this page before you move on. When the screen shows ding after Bell.ring();, the import line did its job for this lesson and you are ready for the next topic.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top