Task 4 of 8 · 0 solved · 8 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

3.1 Methods — declaring a void method

Lesson

The parts of a method

Declaring a method has four parts: a return type (use void when it returns nothing), a name, a pair of parentheses (), and a body in braces { }. void means the method does its work but hands back no value.


Worked example

void printLine() {
    System.out.println("-----");
}

Return type void, name printLine, empty (), and a body that prints a row of dashes. Calling printLine() runs that one line.


▶ Try it: printLine() is defined for you and prints a row of dashes. Run this, then change how many times it is called.



In void printLine() { ... }, what does void mean?


Put the parts of the method header in the order they are written: void printLine().


Match each part of a method to its role.


True or false?


Type exactly what this program prints.

public class Main {
    static void star() {
        System.out.println("*");
    }
    public static void main(String[] args) {
        star();
    }
}