CCA Guide
Methods

Methods

Curated by Last modified: 1 min read

View Markdown

Definition

This method prints Hi! and returns 0:

public int hello() {
    System.out.println("Hi!");
    return 0;
}

It belongs inside a class. General schematic syntax:

accessModifier returnType name(parameters) {
    body
}

The declared return type determines the allowed return value; void means no value is returned.

Terminology clarification

A method’s signature includes its name and parameter types, not its return type. Thus changing only the return type cannot create an overload.

Source: Java Language Specification §8.4.2.

Related: Classes and overloading · Recursion

Linked to this page