Loading...

Java static

Write a class Tag in Tag.java with a static method static void ping() that prints ok. In Yard.java, call Tag.ping(); from main with no new Tag() on this page. Compile both files and run java Yard. The screen should show one line: ok. The method belongs to the class name Tag, not to one object variable. Calling through the class name is what this page teaches.

A method on the class name

On the instance-methods page you wrote j.bump() after new Jar(3). That call needed an object. A static method does not. You write Tag.ping(); using the class name. Java runs that method without building a Tag object first on this page.
Create Tag.java first. The public class line is public class Tag. Do not leave an older Jar or Tin class name here. Inside the braces write one method:
static void ping() { System.out.println("ok"); }
There is no field and no constructor on this first demo. There is no main in Tag.java. Save before you compile. The word static sits in the method header before void. Without static, a later Tag.ping(); call from Yard will not match the instance-method shape this page wants.

Tag with static ping

java
public class Tag {
    static void ping() {
        System.out.println("ok");
    }
}

Yard calls Tag.ping

Create Yard.java in the same folder. Its public class line is public class Yard. Inside main, write one call: Tag.ping();. Do not write Tag t = new Tag(); on this page. Do not write t.ping(); as the lesson path. The class name before the dot is enough.
Keep Yard as the class that holds main. Do not put main inside Tag for this lesson. If a leftover Desk file from the instance-methods page still sits in the folder, do not run java Desk for this lesson — run java Yard after you compile Tag.java and Yard.java.

Yard calls Tag.ping

java
public class Yard {
    public static void main(String[] args) {
        Tag.ping();
    }
}

From the folder that holds both files, run javac Tag.java Yard.java. You can compile Tag.java first and Yard.java second when both are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java, and not java Tag. Leave the .java ending off the run command. Start the class that holds main. The same commands work on Windows and on macOS.
The program should print one line and stop: ok. 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 Tag.ping(); in Yard, with static void ping in Tag.
Walk the run once. Java starts at Yard.main. The call Tag.ping() finds the static method on class Tag and runs its body. The body prints ok. No object was built. The screen shows ok. Nothing else should print. If you see 4 or 5, you are still running an older Desk program from the bump lesson.
If you write ping(); with no Tag. in front inside Yard.main, javac refuses in this shape. The static method still needs its class name (or a careful import style you are not using yet). Keep Tag.ping();. If you add void without static, then Tag.ping(); is the wrong kind of call for an instance method — put static back. The header must look like static void ping(), not void ping() alone, and not static int ping() for this print demo.

Tag.ping and j.bump side by side

One short table keeps the two call shapes straight.

📊 static vs instance call

Call Needs an object first?
Tag.ping(); no — class name is enough
j.bump(); yes — after Jar j = new Jar(3);
static void ping() method on the class
void bump() method on each object
The table is only a reminder. The typed line you keep on this page is Tag.ping(); in Yard, with static void ping() in Tag.

Compared with instance methods

On the instance-methods page, j.bump() changed fill on one jar. That still works. This page does not change a field. It only prints ok from a static method so you can see the class-name call. A leftover Jar j = new Jar(3); j.bump(); demo is a different lesson. For this page keep Tag and Yard and Tag.ping();.
You can still build objects on other days. This lesson chooses the static path on purpose. Do not mix j.bump() and Tag.ping() in the same Yard demo on this page. One clear path is enough: static on the method, class name before the call, print ok. When you later need both styles in one program, keep each call honest — object before instance methods, class name before static methods — so readers can see which kind you meant.

You do not need new Tag for this call

Because ping is static, Yard does not allocate a Tag object before the call. Writing Tag t = new Tag(); and then t.ping(); may compile for a static method in many setups, but that is not the shape this page teaches. Stay with Tag.ping(); alone. If the screen shows 4 or 5, an older Desk bump demo is still running — switch to java Yard.
If javac says it cannot find symbol ping on Tag, check that the method header includes static and that both files sit in the same folder. If java Tag runs and does nothing useful, you started the wrong class — start Yard, which holds main.

What this leaves for access

This page uses a public class and a package-visible static method in the same folder. A later page — access — shows words like public and private on members. Stay with the static call here. Do not invent private static on this page unless you already know it; the lesson goal is Tag.ping(); with static void ping().
When the folder still holds Jar.java or Desk.java, leave them alone or delete them only if they confuse your compile command. Run java Yard after you compile the Tag and Yard pair from this lesson.

Before access

You can write static void ping() { System.out.println("ok"); } in Tag, and from Yard call Tag.ping(); with no object. Compile both files and run java Yard. The screen shows ok. The static method ran on the class name. The next page is access, where you control which names other classes may see.
Keep static on the method. Keep the class name before the call. Keep main in Yard. Those three habits are the checkpoint for this page before you move on.

🧠 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