InfoparkDailyCAREER TOOLS

Java · Lesson 5 of 9

Methods

Defining methods, parameters, return types and overloading.

A method is a named block of code inside a class. Its signature lists the return type, name and parameters:

static double monthlySalary(double lpa) {
    return lpa * 100000 / 12;
}
  • void means the method returns nothing.
  • static methods belong to the class and can be called without creating an object (useful in main).
  • Overloading: several methods with the same name but different parameter lists.
  • Java passes arguments by value — for objects, the value is a copy of the reference.

Example program

Run with JDK 17+: java Main.java
public class Main {
    static double monthlySalary(double lpa) {
        return lpa * 100000 / 12;
    }

    static String greet(String name) {
        return "Hello, " + name;
    }

    static String greet(String name, String city) {   // overload
        return "Hello, " + name + " from " + city;
    }

    static boolean isEligible(int age, double cgpa) {
        return age >= 18 && age <= 28 && cgpa >= 6.5;
    }

    public static void main(String[] args) {
        System.out.printf("Monthly: %.2f%n", monthlySalary(6));
        System.out.println(greet("Rahul"));
        System.out.println(greet("Rahul", "Thrissur"));
        System.out.println(isEligible(23, 7.2));
    }
}

Output

Monthly: 50000.00
Hello, Rahul
Hello, Rahul from Thrissur
true

Save the code as Main.java and run java Main.java (JDK 11+ runs single files directly). Names and figures are fictional.

Key points

  • Signature = return type + name + parameters.
  • void returns nothing.
  • Overloading = same name, different parameters.

Quick check

What does the return type void mean?

Practice task

Write a method maxOf(int a, int b, int c) that returns the largest of three numbers, and call it from main.

Interview corner

Is Java pass-by-value or pass-by-reference?

Always pass-by-value. For objects, the reference is copied, so a method can change the object's fields but cannot make the caller's variable point to a different object.

Overloading vs overriding?

Overloading: same method name, different parameters, in the same class (compile-time). Overriding: a subclass redefines a parent method with the same signature (run-time polymorphism).

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.