AP-Computer-Science ScienceAP
Methods Methods
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