You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Methods let you break code into reusable, named blocks. Arrays let you store multiple values of the same type in a single variable. Together they are foundational building blocks for every Java program.
A method has a return type, a name, and zero or more parameters:
public static int add(int a, int b) {
return a + b;
}
accessModifier static returnType methodName(parameterList) {
// body
}
| Part | Example | Meaning |
|---|---|---|
public | Access modifier | Visible everywhere |
static | Belongs to class | Can be called without creating an object |
int | Return type | The method returns an integer |
add | Method name | How you call the method |
(int a, int b) | Parameters | Input values |
int result = add(3, 7); // result = 10
Methods that do not return a value use void:
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.