Loading...

Java Methods

Write a method named ring that prints one word: ding. Put those print lines under static void ring() with empty parentheses. Inside main, write only ring(); — the name, then empty parentheses, then a semicolon. Compile and run. The screen should show one line: ding. main does not hold the println for ding. The method does. That split is what this page teaches.

Lines that live under ring

Until now, most of your print work sat directly inside main. That still works. A method gives the same lines a name of their own. You write the lines once under that name. Then main asks for them by writing the name as a call.
Create a small program file named Chime.java. The public class on the first line must match the file name: public class Chime. Do not leave an older Pack or Skip class name in this file; the compiler expects the class name and the file name to agree.
Outside main, still inside the class braces, write:
static void ring() {
System.out.println("ding");
}
The word static stays on this beginner shape so main can call ring without building an object. The word void means this method does not hand a value back to the caller — it only runs its body. The name ring is the label you will type in main. The empty parentheses () mean this page’s method takes no values in. The braces hold the body. System.out.println("ding"); writes the four letters d-i-n-g. Save the file before you compile.

Chime.java you compile

Here is the full small program for this page. main holds only the call. ring holds the print.

ring prints ding

java
public class Chime {
    public static void main(String[] args) {
        ring();
    }

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

From the folder that holds Chime.java, run javac Chime.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Chime — not java Chime.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not need a different shell spelling for either system. 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 the file. Delete that call and keep main’s ring(); plus the ring method above. A wrong screen line does not mean you need a new JDK install.

Walk the run once in order. Java starts at main. The only statement in main is ring();. That call jumps into the ring method. Inside ring, println writes ding. Then ring finishes, and main has nothing left, so the program ends. You did not put System.out.println("ding"); inside main on this page. You put it under ring and asked for it by name. If the screen shows ding, the call reached the body. If the screen stays blank after a clean compile, main never called ring, or main still holds only empty braces with no call inside.

Call ring from main more than once

Change only main so it calls the same method twice:
public static void main(String[] args) {
ring();
ring();
}
Leave the ring method itself alone. Compile and run again: javac Chime.java, then java Chime. The screen should show two lines, each saying ding. The first call runs the body once. The second call runs the same body again. That reuse is the reason to name the lines. You did not copy the println a second time under ring. You wrote a second call in main.
If you put the second ring(); back out and leave only one call, the screen returns to a single ding. The method definition does not print by itself. Only a call from main (or from another method later) runs the body. A file that defines ring but never calls it from main can compile and still show a blank screen when you run java Chime, because main has nothing to do.

Pieces of a method header

On this page the header is static void ring(). Read it left to right. static lets main call the method on this beginner shape. void means no value comes back through the call. ring is the name. () are empty on purpose: this session does not pass words or numbers into the method. The next page, parameters, puts names inside those parentheses. The page after that, return, changes void when you need a value back. Keep both of those for later. Today the empty () and void are enough.

📊 Header piece on this page

Piece On this page
static main can call ring without an object
void ring does not hand a value back
ring the name you type in the call
() no values go in on this page
{ ... } the body that prints ding

The call in main must match the name exactly: ring(); with a lowercase r, empty parentheses, and a semicolon. Writing Ring(); with a capital R is a different name to Java. Writing ring without () is not a call on this shape. Writing System.out.println("ring"); prints the four letters of the word ring; it does not run the method body. Keep the call as ring(); when you want ding on the screen.

A common leftover is an old Pack for-each demo still sitting in main beside the new call. Then the screen may show 2, 4, and 6, or red and blue, instead of ding — or both kinds of lines mixed. Clear main down to the ring(); calls this lesson needs. Another mix-up is putting the method inside main’s braces. Methods on this page sit inside the class, next to main, not nested inside main. javac will not treat a method buried inside main as the ring you call. Keep ring’s braces closed before main ends, and keep main’s braces closed before the class ends, so each block owns only its own lines.

Ready for the next pages

You can write static void ring(), put println lines in its braces, and call ring() from main so the screen shows ding. Calling twice prints ding twice without copying the body. Empty parentheses and void stay on this page on purpose. Parameters will put values inside the parentheses. Return will let a method hand a number or text back to main. Those are the next two pages after this one.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

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