Menu
Coddy logo textTech

Declaring Methods

Part of the Fundamentals section of Coddy's Java journey — lesson 51 of 73.

A method is a sequence of code that has a name. The purpose of a method is to reuse a piece of code multiple times.

For example, take a look at this code:

System.out.println("Welcome to Coddy");
System.out.println("New session...");
System.out.println("Welcome to Coddy");
System.out.println("Another session...");
System.out.println("Welcome to Coddy");

We use the same code System.out.println("Welcome to Coddy") over and over again. Another issue with this code is that if we wanted to change the message: Welcome to Coddy to something different, like "Welcome aboard" it would have to change 3 different lines of code. To solve this issue, we will use methods.

To declare a method, we use the following syntax:

access_modifier return_type method_name(parameters) {
	// code
}

For our example, we will create a method named greet and it will look like this:

public static void greet() {
    System.out.println("Welcome to Coddy");
}

To use/call/execute the method, we write greet();:

greet();
System.out.println("New session...");
greet();
System.out.println("Another session...");
greet();

This will result in the same output as above.

Note: In Java, a method does not need to appear before its call in the source file — the compiler resolves method references across the entire class. As a convention, however, it is common to place helper methods after the code that calls them, or to organize them in a consistent order for readability.

challenge icon

Challenge

Easy

Write a program that gets one input, a number. The input number indicates how many times to execute the below method. 

Create a method that calculates the sum of all of the numbers between 1 and 1000 (including) and prints it, name the method however you like.

Note! As a good practice for readability, write the method before its call/execution statements in your code.

Cheat sheet

A method is a sequence of code that has a name, used to reuse code multiple times.

Method syntax:

access_modifier return_type method_name(parameters) {
    // code
}

Example method declaration:

public static void greet() {
    System.out.println("Welcome to Coddy");
}

To call/execute a method:

greet();

Note: In Java, a method does not need to appear before its call in the source file — it just needs to exist in the class. However, it is a common convention to declare methods before calling them for readability.

Try it yourself

import java.util.Scanner;

public class Main {
    // Method declaration
    public static void sumNumbers() {
        // Complete Method
        
    }
    
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            // Call the method n times
        }
        
        scanner.close();
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals