Lesson 7: Methods (Functions)
A method is a block of code that performs a specific task. By using methods, you can break
your program into smaller, more manageable pieces, making your code easier to read, debug,
and reuse.
Think of a method as a named recipe. You can call the recipe by its name whenever you need
to perform that task.
Step 1: Defining a Method
A method definition has a few key parts:
modifier returnType methodName(parameters) { // Method body }
● modifier: This specifies the access level of the method. For now, we'll use public static.
(public means the method can be called from anywhere, and static means it belongs to
the class itself, not an object.)
● returnType: This is the data type of the value the method will return. If the method
doesn't return anything, you use the keyword void.
● methodName: This is the name of your method. By convention, it should start with a
lowercase letter and use camelCase (e.g., calculateSum).
● parameters: A comma-separated list of variables that the method accepts as input. If
there are no parameters, you just use empty parentheses ().
Example: A Method That Returns a Value
Java
public class Lesson7 {
// A method that takes two integers and returns their sum
public static int addNumbers(int a, int b) {
int sum = a + b;
return sum; // The 'return' keyword sends the result back
}
public static void main(String[] args) {
// We will call the method here
}
}
● This addNumbers method takes two int parameters (a and b) and returns an int result.
Step 2: Calling a Method
To use a method, you "call" or "invoke" it from another part of your code (usually from the
main method).
Example: Calling the addNumbers Method
Java
public class Lesson7 {
public static int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
public static void main(String[] args) {
// Call the method and store the result in a variable
int result = addNumbers(5, 10);
[Link]("The sum is: " + result); // Output: The sum is: 15
// You can also call it directly inside a print statement
[Link]("The sum is: " + addNumbers(20, 30)); // Output: The sum is: 50
}
}
Step 3: A Method That Doesn't Return a Value (void)
If a method's job is just to perform an action (like printing something) and it doesn't need to
send a value back, its return type is void.
Example: A Method That Prints a Greeting
Java
public class Lesson7 {
// A method that takes a name and prints a greeting
public static void sayHello(String name) {
[Link]("Hello, " + name + "!");
}
public static void main(String[] args) {
sayHello("Alice"); // Call the method
sayHello("Bob"); // Call it again with a different parameter
}
}
Output:
Hello, Alice!
Hello, Bob!
Step 4: Your Task
1. Open your project in Eclipse and create a new class named Lesson7.
2. Inside this class, define a new public static method named calculateAverage.
○ This method should accept an array of double values as its parameter.
○ It should return a double value, which is the average of the numbers in the array.
○ Inside the method, use a loop to calculate the sum of the array elements, then
divide by the number of elements (use [Link]).
3. Inside the main method:
○ Create a double array with at least 5 values.
○ Call your calculateAverage method and pass your array to it.
○ Store the returned average in a variable.
○ Print the average to the console.
4. Challenge: Write another method named findMax that takes an array of int as a
parameter and returns the largest value in the array. Call this method from main and
print the result.
Methods are the foundation of object-oriented programming in Java. They are essential for
writing clean, modular, and reusable code. Once you're comfortable with this concept, you're
ready for the next big step: Object-Oriented Programming (OOP)!