Lesson 30 of 50 – Java Methods
60%

Java Methods

A method in Java is a block of code that performs a particular task. You can call a method whenever you need it, which helps reduce repeated code and makes programs easier to organize.

1. What Is a Method?

Consider a program that prints a welcome message. Instead of writing the same code repeatedly, you can create a method.

public class Main {

    static void greet() {
        System.out.println("Welcome to Java!");
    }

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

Output:

Welcome to Java!
Welcome to Java!
A method runs when it is called. Defining a method alone does not execute its body.
2. Syntax of a Method
accessModifier returnType methodName(parameters) {
    // Method body
}
Part Meaning
Access modifier Controls where the method can be accessed.
Return type Specifies the type of value returned, or void if no value is returned.
Method name The name used to call the method.
Parameters Optional input variables received by the method.
Method body Contains the statements executed by the method.
3. Creating and Calling a Method

Create a simple method and call it from main().

public class Main {

    static void display() {
        System.out.println("This is a Java method.");
    }

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

Output:

This is a Java method.

The method is named display(). The statement display(); calls it.

4. Method Without Parameters

A method without parameters does not receive input values from the caller.

public class Main {

    static void showMessage() {
        System.out.println("Welcome to Soopro Pathshala!");
    }

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

Output:

Welcome to Soopro Pathshala!

The empty parentheses in showMessage() indicate that the method has no parameters.

5. Method with Parameters

Parameters allow a method to receive values when it is called.

public class Main {

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

    public static void main(String[] args) {
        greet("Amit");
        greet("Rahul");
    }
}

Output:

Hello, Amit
Hello, Rahul

Here, name is a parameter. The strings "Amit" and "Rahul" are arguments supplied when the method is called.

6. Method with Multiple Parameters

A method can receive more than one parameter.

public class Main {

    static void studentInfo(String name, int age) {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        studentInfo("Amit", 20);
        studentInfo("Priya", 19);
    }
}

Output:

Name: Amit
Age: 20
Name: Priya
Age: 19

The arguments must match the parameter types and order required by the method.

7. Method That Returns a Value

A method can calculate a value and return it using the return statement.

public class Main {

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

    public static void main(String[] args) {
        int result = add(10, 20);

        System.out.println("Sum = " + result);
    }
}

Output:

Sum = 30

The method returns an integer. The returned value is stored in the variable result.

8. Different Return Types

Methods can return values of different types.

public class Main {

    static int getAge() {
        return 20;
    }

    static double getPrice() {
        return 99.50;
    }

    static String getName() {
        return "Amit";
    }

    static boolean isPassed() {
        return true;
    }

    public static void main(String[] args) {
        System.out.println(getAge());
        System.out.println(getPrice());
        System.out.println(getName());
        System.out.println(isPassed());
    }
}

Output:

20
99.5
Amit
true

The returned value must be compatible with the method's declared return type.

9. Void Methods and Return Methods
Void Method Return Method
Does not return a value. Returns a value of a specified type.
Uses the void keyword. Uses a type such as int or String.
static void greet() static int add()

Void method:

static void greet() {
    System.out.println("Hello!");
}

Return method:

static int square(int number) {
    return number * number;
}

Use a void method when you need an action performed. Use a return method when you need a result to use elsewhere.

10. Calculate Area Using a Method

Calculate the area of a rectangle using a method.

public class Main {

    static int calculateArea(int length, int width) {
        return length * width;
    }

    public static void main(String[] args) {
        int area = calculateArea(10, 5);

        System.out.println("Area = " + area);
    }
}

Output:

Area = 50

The method receives the length and width, multiplies them, and returns the area.

11. Static and Instance Methods

A static method belongs to the class. An instance method is called on an object of that class.

public class Main {

    static void staticMethod() {
        System.out.println("Static method");
    }

    void instanceMethod() {
        System.out.println("Instance method");
    }

    public static void main(String[] args) {

        staticMethod();

        Main obj = new Main();
        obj.instanceMethod();
    }
}

Output:

Static method
Instance method

A static method can be called directly from the same class. An instance method normally requires an object to call it.

12. Method Overloading

Method overloading means defining multiple methods with the same name but different parameter lists.

public class Main {

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

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

    static double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(10, 20));
        System.out.println(add(10, 20, 30));
        System.out.println(add(2.5, 3.5));
    }
}

Output:

30
60
6.0

Overloaded methods must differ in their parameter lists. Changing only the return type is not enough.

13. Practice Assignments

Write Java programs to complete these assignments.

  1. Create a method that prints "Welcome to Java".
  2. Create a method that accepts a student's name and prints it.
  3. Create a method that accepts two integers and returns their sum.
  4. Create a method that returns the square of a number.
  5. Create a method that returns the largest of two integers.
  6. Create a method that checks whether a number is even.
  7. Create a method that calculates the area of a rectangle.
  8. Create a method that converts Celsius to Fahrenheit.
  9. Create overloaded methods to add two and three numbers.
  10. Create a class containing one static method and one instance method.
Tip: Practice method declaration, parameters, return values, and method calls before moving to more advanced method concepts.

🧠 Quick Quiz

1. Which keyword indicates that a method does not return a value?




2. Which statement sends a value back from a method?




3. What is method overloading?