Loading...

Java Classes

A class is a named type you declare in a .java file. Put public class Badge in a file named Badge.java. Inside that type write a short static method show that prints ready. In a second file named Desk.java put public class Desk with a main that calls Badge.show();. Compile both files, then run java Desk. The screen should show one line: ready. You are not counting down with recursion here. You are naming a type in its own file and asking that type’s method to print. That second named type — a class that is not only the file that starts — is what this page teaches.

The type lives in its own file

Until the methods pages, almost every demo kept main and the helper methods inside one public class. That still works. This page adds a second public class on purpose. Badge is a type name. It does not need to be the program that starts. Desk is the program that starts. Each public class sits in a file whose name matches the class name exactly, including capital letters.
Create Badge.java first. The first line of the type is public class Badge. Do not write public class Desk inside this file. Do not leave an older Ladder or Gate class name here. The compiler ties the public class name to the file name. If the file is Badge.java and the line says public class Ladder, javac refuses.
Inside the braces of Badge, write static void show(). The body is one print: System.out.println("ready");. Keep void on this page. The method does not return a number. It only writes a word. There is no main in Badge.java on this page. That is intentional. A class can hold methods without being the starting class.
Save Badge.java before you compile. The first boxed program is that file alone.

Badge type with show

java
public class Badge {
    static void show() {
        System.out.println("ready");
    }
}

Desk starts; Badge does the print

Create a second file in the same folder: Desk.java. Its public class line is public class Desk. Inside main, write one call: Badge.show();. That call names the type Badge, a dot, then the method show. You are not writing show(); alone inside Desk unless show also lives in Desk. On this page show belongs to Badge, so the call carries the type name.

Desk main calls Badge.show

java
public class Desk {
    public static void main(String[] args) {
        Badge.show();
    }
}

Both files should sit in the same folder so javac can see them together. From that folder run javac Badge.java Desk.java. You can also compile Badge.java first and Desk.java second; both orders work when the files are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Desk — not java Desk.java, and not java Badge. Leave the .java ending off the run command. Start the class that holds main. The same two kinds of commands work on Windows and on macOS.
The program should print one line and stop: ready. 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 Badge.show(); in Desk plus the show method in Badge.
Walk the run once. Java starts at Desk.main. The call Badge.show() enters the method in the Badge type. That method prints ready and returns. Desk then ends. The word on the screen came from Badge, not from a println written inside Desk itself. That split is the point: one class starts the program; another class owns the helper.

File name and public class must agree

Read the pairing once in a small table. Each public class owns one file name.

📊 file, public class, role

File public class Role on this page
Badge.java Badge type that holds show
Desk.java Desk program that holds main

If you rename the file to badge.java with a small b while the line still says public class Badge, some systems treat the mismatch as an error. Keep the capital B in both places on this beginner shape. If you put public class Badge and public class Desk in the same file, javac refuses: one public class per file. Private helper classes in the same file wait for later courses. Here you keep two public files.
A common mix-up is running java Badge because you just wrote Badge.java. That class has no main on this page, so the run fails to start. Run java Desk. Another mix-up is writing show(); inside Desk without the Badge. prefix when show only exists on Badge. The compiler then looks for show inside Desk and does not find it.
Leftover recursion demos can confuse the folder. A file still named Ladder.java with down(3) is a different program. Keep this lesson’s folder focused on Badge.java and Desk.java while you learn the two-file shape. You do not need down here.

Compile one type, then the starter

You can practice the compile step in smaller pieces. First compile only the type: javac Badge.java. That command checks Badge even though nothing has started yet. A class file can compile without a main. Then compile the starter: javac Desk.java. If Badge is already compiled in the same folder, Desk can resolve the name Badge in the call. Finally run java Desk and confirm the single line ready.
If javac Desk.java says it cannot find the symbol Badge, check three things. The folder must hold Badge.java or the compiled Badge class beside Desk.java. The call must spell Badge with the same capital letters as the type. The method name after the dot must be show, matching the header in Badge.
Writing System.out.println("Badge"); inside Desk prints the word Badge. It does not call the method on the type. Keep Badge.show(); when you want the print that lives in Badge.

What this page does not do yet

This page does not write new Badge(). Creating an object from a class is the next page. Here the call is Badge.show() — a static method on the type name itself. You also do not store fields on a badge yet. Fields wait for a later page in this module. The job now is only the named type in its own file, the matching public class line, and a call that uses that type name.
You can change the printed word inside show to set and recompile both files (or at least Badge.java, then run Desk again). The screen should follow the string you put in Badge. Desk stays the same call. That is another way to see the split: edit the type’s method, keep the starter’s call.

Before objects

You can declare public class Badge in Badge.java, put static void show() inside it, and call Badge.show(); from Desk.main. File name and public class name match. Compile the type and the starter, then run java Desk. The screen shows the line printed by show. A class is that named type. The next page is objects, where you build a value from a class with new.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

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