Methods
Hub › Java › Beginner › Methods
Goal
Write a Java method that takes parameters and returns a value. After this page you will be able to read the controller methods on later pages.
Prerequisites
What a method looks like
A method is a named block of code that takes zero or more inputs and returns one value (or nothing, written as void). It always lives inside a class.
The shape of a method declaration:
<visibility> <return-type> <name>(<parameter list>) { ... }- Visibility is
public,private, or omitted.publicmeans any other class can call it. - Return type is the type of the value the method gives back. Use
voidif it gives nothing back. - Parameter list is zero or more
Type namepairs, separated by commas.
The return keyword exits the method and hands a value back to the caller. If the return type is void, omit the value.
Code
java
public class Greeter {
public static String greet(String name, int year) {
return "hello " + name + ", year " + year;
}
public static void main(String[] args) {
var message = greet("Aarav", 2026);
System.out.println(message);
}
}Run it:
bash
java Greeter.javaOutput:
hello Aarav, year 2026What changed from the previous page:
greetis a method that takes two parameters and returns aString.maincallsgreetand stores the result inmessage.staticon both methods means they belong to the class itself, not to an instance. That is what letsmaincallgreetwithout first creating aGreeterobject.
Next → Records