Loading...

Java Packages

Put class Bell in a folder named kit with the first line package kit; and a static method ring() that prints ding. In Lobby.java beside that folder — not inside kit — call kit.Bell.ring(); from main. Compile from the parent folder and run java Lobby. The screen should show one line: ding. The package name and the folder name match. Calling with kit.Bell in front is what this page teaches. Leave the word import for the next page.

A folder name that the file declares

Until now every class sat in one flat folder. A package groups classes under a folder name. The first line of the file must say that name: package kit;. The file Bell.java must live inside a folder called kit. If the folder and the package line disagree, javac complains.
Create the folder kit first. Inside it create Bell.java. The first line is package kit;. Then the public class line is public class Bell. Do not leave an older Gate or Tag class name here. Write one static method:
public static void ring() { System.out.println("ding"); }
Use public on the method so a class outside the package can call it. There is no main in Bell.java. Save before you compile.

Bell in package kit

java
package kit;

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

Lobby calls kit.Bell.ring

Create Lobby.java in the parent folder — the same level as the kit folder, not inside kit. Its public class line is public class Lobby. Inside main, write kit.Bell.ring();. Do not write import kit.Bell; on this page. The full name kit.Bell is enough for the call.
Keep Lobby as the class that holds main. Do not put main inside Bell for this lesson. If a leftover Pier file from the access page still sits nearby, do not run java Pier for this lesson — run java Lobby after you compile from the parent folder.

Lobby calls kit.Bell.ring

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

From the parent folder that holds both Lobby.java and the kit folder, run javac kit/Bell.java Lobby.java. On Windows the slash can be kit\Bell.java in the same idea — point javac at the file inside the package folder, then at Lobby.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Lobby — not java Lobby.java, and not java kit.Bell. Leave the .java ending off the run command. Start the class that holds main. The same commands work on Windows and on macOS once the paths match your shell.
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 kit.Bell.ring(); in Lobby, with package kit; in Bell.
Walk the run once. Java starts at Lobby.main. The call kit.Bell.ring() finds class Bell in package kit and runs the static method. 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 Bell.java sits next to Lobby.java with no kit folder while the file still says package kit;, javac refuses. Move the file into kit. If you call Bell.ring(); without kit. from outside the package, javac cannot see the short name on this page — keep kit.Bell.ring();. The package name is part of the type’s full name until the next page teaches import.

Folder and package line side by side

One short table keeps the match clear.

📊 folder vs package line

Piece On this page
folder name kit
first line in Bell.java package kit;
call from Lobby kit.Bell.ring();
import not on this page — next lesson
The table is only a reminder. The typed pieces you keep are package kit; in the Bell file, the file path under kit, and kit.Bell.ring(); in Lobby.

Compared with access and with a flat folder

On the access page, Gate and Pier shared one folder with a private field. That still works. This page adds a package folder and a package line. A leftover Gate g = new Gate(9); g.show(); demo is a different lesson. For this page keep kit/Bell.java and Lobby.java and kit.Bell.ring();.
You can still use flat folders for tiny demos. This lesson chooses a package so you see the name in front of the class. Do not mix g.show() and kit.Bell.ring() in the same Lobby demo on this page. One clear path is enough: package line, matching folder, full call from Lobby.

Compile from the parent, not from inside kit alone

Stay in the parent folder when you compile and when you run java Lobby. If you cd into kit and try to run Lobby from there without fixing the classpath story, the short lesson path breaks. Keep the parent as your working folder for this page. The .class file for Bell should appear under kit after a good compile, beside Bell.java.
If the screen shows 9 or ok, an older Pier or Yard program is still running. Switch to java Lobby. If javac cannot find kit.Bell, check the folder name spelling and the package kit; line character by character.

What this leaves for import

This page types the full name kit.Bell at every call. A later page — import — shows how import kit.Bell; lets you write the short name Bell in Lobby. Stay with the full name here. Do not invent import on this page unless you already know it; the lesson goal is the package folder and kit.Bell.ring();.
When old Gate.java or Pier.java files still sit in the parent folder, leave them alone or delete them only if they confuse your compile command. Run java Lobby after you compile the package pair from this lesson.

Before import

You can place Bell.java under folder kit with package kit; and public static void ring() that prints ding, and from Lobby call kit.Bell.ring(); with no import. Compile from the parent folder and run java Lobby. The screen shows ding. The package name matched the folder. The next page is import, where a short name becomes enough after an import line.
Keep package kit; as the first line in Bell. Keep the file inside kit. Keep the call as kit.Bell.ring();. Those three habits are the checkpoint for this page before you move on. When the screen shows ding from the parent folder, the package layout is doing its job for this lesson.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

❓
6
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