Java Lesson 12 - Methods | Dataplexa

Methods in Java

As programs grow, writing everything inside the main method becomes messy. Large programs become hard to read, hard to fix, and hard to reuse.

Java solves this problem using methods. Methods help break a big problem into smaller, manageable pieces.


What Is a Method?

A method is a block of code that performs a specific task. Instead of writing the same logic again and again, you write it once inside a method and reuse it whenever needed.

Think of a method like a machine: you give it input, it processes the input, and it gives you output.


Why Methods Are Important

Methods make programs:

  • Cleaner and easier to read
  • Reusable across different parts of the program
  • Easier to test and debug
  • More professional and maintainable

In real-world software development, methods are used everywhere.


Basic Structure of a Method

Every method in Java has a structure. It defines what the method does and how it can be used.

A method usually includes:

  • Return type
  • Method name
  • Parameters (optional)
  • Method body

Example: Creating a Simple Method

Let’s create a method that prints a welcome message. Instead of writing the message multiple times, we place it inside a method.

public class MethodExample {

    static void displayMessage() {
        System.out.println("Welcome to Java programming");
    }

    public static void main(String[] args) {
        displayMessage();
        displayMessage();
    }
}

Here, the method displayMessage() is called twice. The code inside the method executes each time it is called.


Methods with Parameters

Methods become more powerful when they accept input. This allows the same method to work with different values.

Below is an example where a method accepts a name as input.

public class ParameterExample {

    static void greetUser(String name) {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args) {
        greetUser("Alice");
        greetUser("Bob");
    }
}

The same method produces different output based on the input provided.


Methods That Return Values

Some methods perform calculations and return results. These results can be stored or used elsewhere in the program.

Here’s a method that adds two numbers and returns the result.

public class ReturnExample {

    static int addNumbers(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int sum = addNumbers(10, 20);
        System.out.println(sum);
    }
}

This approach is widely used in calculations, validations, and business logic.


Real-World Use of Methods

Methods are used in real applications to:

  • Validate user input
  • Calculate totals, tax, or discounts
  • Handle database operations
  • Organize business rules

Almost every professional Java application relies heavily on methods.


What You Learned in This Lesson

  • What methods are and why they are needed
  • How to create and call methods
  • How to pass data using parameters
  • How methods return values

In the next lesson, you will learn about method overloading, which allows multiple methods to share the same name.