bhylu.com

Advertisements

Java Name a Reusable Operation

A method gives a small operation a name, inputs and a result contract. Its value comes from a clear responsibility, not from moving arbitrary lines out of main.

Understand the idea

The method declaration states a return type, name and parameters. A caller supplies argument values. A static helper belongs to the class and can be called from static main without first constructing an object. Later object methods will act on instance state. Keep a method’s decision visible through its name and result instead of relying on hidden global variables.

Run it locally

Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.

JDK and terminal setup

javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out Main

A worked example

Java 21+ · Main.java

public class Main {
    static int availableSeats(int capacity, int reserved) {
        return capacity - reserved;
    }

    public static void main(String[] args) {
        System.out.println(availableSeats(20, 6));
        System.out.println(availableSeats(12, 5));
    }
}

Expected output

14
7

Each call has independent parameter variables. The helper calculates an answer and returns it; it does not print or update a reservation system. That makes it easier to reuse from a console, test or future web boundary. The example assumes validated inputs so the arithmetic responsibility stays clear.

Check the official Java documentation for detailed contracts and supported options.