Methods in Java
In this lesson: Create and call methods in Java — parameters, return types, static methods and overloading — with output. In Hindi & English.
What is a method
A method is a named block of code that performs a task. You write it once and call it whenever you need it, avoiding repeated code. Even main is a method. Methods break a big program into small, reusable, easy-to-read pieces.
Defining a method
public class Main {
static void greet() {
System.out.println("Hello!");
}
public static void main(String[] args) {
greet(); // call the method
greet(); // call again
}
}
Hello!
We define greet() once, then call it twice from main. The code inside runs each time it is called.
Parameters
static void greet(String name) {
System.out.println("Hello, " + name);
}
// call:
greet("Aman");
greet("Diya");
Hello, Diya
Parameters let you pass information in. name is a parameter; "Aman" is the argument you pass. The method uses whatever value it receives.
Returning a value
static int add(int a, int b) {
return a + b;
}
// call:
int sum = add(5, 3);
System.out.println(sum);
A method can send a value back with return. Its return type (int) must match. Here add returns the sum, which we store in sum.
void vs return
- void — the method does something but returns nothing (like printing). No
returnvalue. - return type — the method gives a value back (like
int,String) usingreturn.
Choose void when the method just performs an action, and a return type when you need a result from it.
Static methods
static int square(int n) {
return n * n;
}
// called without an object:
System.out.println(square(4)); // 16
A static method belongs to the class, so you can call it directly (like square(4)) without creating an object. main is static, so it can call other static methods directly.
Common mistakes
- Return type mismatch. A method declared
intmustreturnan int. - Forgetting
return. A non-void method must return a value on every path. - Wrong argument count or type. Arguments must match the parameters.
- Calling non-static from static without an object. A static
maincannot call a non-static method directly.
Summary
- A method is a reusable named block of code for one task.
- Parameters take input; arguments are the values you pass.
- Use
returnto send a value back; the return type must match. voidreturns nothing; a return type gives a value.staticmethods are called on the class without an object.
Next, learn reuse with the same name in overloading vs overriding, or the static keyword.
Frequently Asked Questions
What is a method in Java?
main is itself a method.What are parameters and arguments?
int a, int b). Arguments are the actual values you pass when calling it (like add(5, 3)). Parameters receive the arguments.What is the difference between void and a return type?
void method does a task but gives nothing back. A method with a return type (like int) sends a value back with return, which you can store or use.What does static mean for a method?
static method belongs to the class, not to an object, so you can call it without creating an object. main is static. Non-static methods need an object to be called on.