Declaring and calling methods
What you will learn
A method is a named unit of behavior that can be called from another part of a Java program. Instead of repeating the same instructions, give them a name and invoke that name whenever the work is needed.
public class Main { static void printTicket() { System.out.println("Ready"); } public static void main(String[] args) { printTicket(); printTicket(); }}Expected output:
ReadyReadyThe same method body executes twice, once per call.
How it works
In static void printTicket() { ... }, static means the method belongs to the class and can be called without creating an instance. The result type void means no value is returned. The name is printTicket, the empty parentheses declare no parameters, and braces enclose the body. Put this declaration inside Main but outside main. Calling printTicket(); transfers control to its body; normal completion returns control to the next statement. Declaring a method does not execute it. A method can call another method, including one declared later in the class. Java methods belong to types; the term function is often used informally. Constructors initialize new objects and are not ordinary methods. Instance methods and constructors are covered in the OOP chapters.
public class Main { static void header() { System.out.println("Dispatch"); } static void report() { header(); System.out.println("Packed"); } public static void main(String[] args) { report(); System.out.println("Finished"); }}Expected output:
DispatchPackedFinishedreport calls header and resumes when header completes. main then resumes after report.
Check a boundary or unusual input
public class Main { static void unused() { System.out.println("Unused"); } static void end() { System.out.println("End"); } public static void main(String[] args) { System.out.println("Start"); end(); }}Expected output:
StartEndThe unused method never executes. Source declaration order does not determine call order.
Common mistakes
Do not declare a named method inside main or add parentheses to a declaration without a result type. A method invocation such as printTicket(); is different from the declaration. Avoid claiming static means constant: static methods can perform side effects. Keep I/O in clearly named methods and do not assume every method returns a value.
Try it yourself
Write a static void method named printLabel that prints Label on one line. For a count from 0 to 4, call it exactly count times using a for loop. Try 3, 0 and 1; do not duplicate the print statement.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Declaring and calling methods — knowledge check
Apply declaring and calling methods to examples and boundary conditions.
Where is a named method declared in this class-based example?
What does void mean?
How often does an uncalled helper run in the example?
After a helper completes normally, where does execution resume?
What does static allow here?