Think of a method as a small machine inside a class.
You give the machine some input → it performs some work → sometimes it gives you an output.
For example:
Input: 10 and 20 Method: adds them Output: 30
Imagine you write this program:
class Demo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = a + b;
System.out.println(c);
int x = 30;
int y = 40;
int z = x + y;
System.out.println(z);
int p = 50;
int q = 60;
int r = p + q;
System.out.println(r);
}
}Here, the same addition logic is repeated.
Instead, we can create a method:
class Demo {
static void add(int a, int b) {
int c = a + b;
System.out.println(c);
}
public static void main(String[] args) {
add(10, 20);
add(30, 40);
add(50, 60);
}
}Now the addition logic is written once and reused many times.
Method = reusable block of code designed to perform a particular task.
Think about a washing machine.
You don't need to know every internal operation.
You simply press:
START
The machine performs many operations internally.
Similarly:
washClothes();The method contains the instructions.
You simply call the method.
Look at:
static void add() {
System.out.println("Addition");
}Break it down:
static → modifier
void → return type
add → method name
() → parameter list
{ } → method body
General syntax:
modifier returnType methodName(parameters) {
// statements
}Example:
static int add(int a, int b) {
return a + b;
}Here:
| Part | Meaning |
|---|---|
static |
method belongs to class |
int |
method returns an integer |
add |
method name |
int a, int b |
parameters |
return a + b |
returned result |
In Java, methods are broadly classified into:
These are methods already provided by Java libraries/classes.
Example:
System.out.println("Hello");println() is a predefined method.
Another example:
Math.max(10, 20);max() is a predefined method.
Another:
String name = "Java";
System.out.println(name.length());length() is a predefined method.
Predefined method = method already provided by Java.
Methods created by the programmer are called user-defined methods.
Example:
class Demo {
static void greet() {
System.out.println("Hello Java");
}
public static void main(String[] args) {
greet();
}
}We created:
greet()Therefore, greet() is a user-defined method.
This is extremely important for exams and interviews.
Depending upon:
- Return type
- Arguments/parameters
we have four types.
METHODS
|
---------------------------
| |
No Return Return
| |
----------- -----------
| | | |
No Arg Arg No Arg Arg
So:
No Return Type + No Arguments
No Return Type + Arguments
Return Type + No Arguments
Return Type + Arguments
Let's learn each one.
Example:
class Demo {
static void add() {
int a = 10;
int b = 20;
int c = a + b;
System.out.println(c);
}
public static void main(String[] args) {
add();
}
}Look at:
void add()Means:
This method does not return a value.
Means:
This method does not receive arguments.
Therefore:
void add()
↓ ↓
no no
return arguments
main()
↓
add()
↓
10 + 20
↓
30
Now suppose we want to give values to the method.
class Demo {
static void add(int a, int b) {
int c = a + b;
System.out.println(c);
}
public static void main(String[] args) {
add(10, 20);
}
}Here:
add(int a, int b)The method accepts two parameters.
When we call:
add(10, 20);the values are passed.
10 → a
20 → b
Then:
a + bbecomes:
10 + 20Output:
30
But the method doesn't return anything.
Therefore:
No Return Type + Arguments
Now we want the method to give a value back.
class Demo {
static int add() {
int a = 10;
int b = 20;
return a + b;
}
public static void main(String[] args) {
int result = add();
System.out.println(result);
}
}Here:
int add()means:
This method returns an
int.
The method executes:
return a + b;which gives:
30
back to the caller.
So:
int result = add();means:
add()
↓
30
↓
result
Output:
30
This is probably the most useful form.
class Demo {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println(result);
}
}Here:
int add(int a, int b)means:
int → returns int
add → method name
int a,b → accepts arguments
Call:
add(10, 20)Execution:
10 → a
20 → b
a + b
↓
30
return 30
↓
result
Output:
30
| Type | Syntax | Example |
|---|---|---|
| 1 | void method() |
void add() |
| 2 | void method(parameters) |
void add(int a, int b) |
| 3 | returnType method() |
int add() |
| 4 | returnType method(parameters) |
int add(int a, int b) |
ARGUMENTS?
/ \
NO YES
| |
NO RETURN NO RETURN
| |
void m() void m(int x)
RETURN?
/ \
NO YES
| |
void method int method()
Even easier:
Arguments = Input Return value = Output
So:
METHOD
|
----------------
| |
INPUT OUTPUT
Arguments Return value
This is another common confusion.
Look at:
static void add(int a, int b) {
System.out.println(a + b);
}Here:
int a, int bare called parameters.
When we call:
add(10, 20);10 and 20 are called arguments.
Parameters are written in method declaration.
Arguments are supplied during method call.
void add(int a, int b)
↑ ↑
parameters
add(10, 20)
↑ ↑
argumentsThis is very important.
Creating a method does not execute it.
Example:
class Demo {
static void hello() {
System.out.println("Hello");
}
public static void main(String[] args) {
}
}Nothing is printed.
Why?
Because we only defined the method.
We didn't call it.
We need:
hello();So:
static void hello() {
System.out.println("Hello");
}means:
"Here is the method."
And:
hello();means:
"Execute this method."
static void hello() {
System.out.println("Hello");
}hello();Think:
Definition = Create the machine
Call = Start the machine
class Demo {
static void hello() {
System.out.println("Hello");
}
public static void main(String[] args) {
hello();
hello();
hello();
}
}Output:
Hello
Hello
Hello
This demonstrates reusability.
That's one of the major advantages of methods.
Suppose we want to perform addition.
For two integers:
add(10, 20);For three integers:
add(10, 20, 30);For two doubles:
add(10.5, 20.5);Can we create methods with the same name?
class Demo {
static void add(int a, int b) {
System.out.println(a + b);
}
static void add(int a, int b, int c) {
System.out.println(a + b + c);
}
static void add(double a, double b) {
System.out.println(a + b);
}
public static void main(String[] args) {
add(10, 20);
add(10, 20, 30);
add(10.5, 20.5);
}
}This is called:
Method overloading is the process of defining multiple methods with the same name but different parameter lists in the same class.
They must differ in their parameter list.
They can differ by:
add(int a, int b)
add(int a, int b, int c)Different number.
add(int a, int b)
add(double a, double b)Different types.
display(int a, double b)
display(double a, int b)Different order.
Changing only the return type is not enough.
This is invalid:
static int add(int a, int b) {
return a + b;
}
static double add(int a, int b) {
return a + b;
}Why?
Because both have the same:
method name → add
parameter list → (int, int)
Only return type changed.
Java cannot overload methods based only on return type.
Method overloading depends on the parameter list, not the return type.
Suppose:
class Demo {
static void show(int x) {
System.out.println("int");
}
static void show(double x) {
System.out.println("double");
}
public static void main(String[] args) {
show(10);
}
}Which method should Java call?
There are multiple show() methods.
Java uses method overloading resolution.
The basic idea is:
show(10);One argument.
So Java looks for methods accepting one argument.
10 is an int.
So:
show(int x)is an exact match.
Therefore:
show(int x)is selected.
Output:
int
For your notes, remember this sequence:
Number of arguments
Type of arguments
Best matching method
In simple cases:
Method Call
↓
1. Number of arguments
↓
2. Type of arguments
↓
3. Best matching method
↓
Execute
Example:
class Demo {
public static void main(String[] args) {
System.out.println("Original main");
main(10);
}
static void main(int x) {
System.out.println("Overloaded main");
}
}Output:
Original main
Overloaded main
So:
main(String[] args)and:
main(int x)are overloaded methods.
The JVM specifically looks for:
public static void main(String[] args)as the entry point.
It does not start execution from:
main(int x)Therefore:
main()can be overloaded, but onlypublic static void main(String[] args)is recognized as the standard JVM entry point.
First, let's see normal methods.
class Calculator {
static int add(int a, int b) {
return a + b;
}
static int subtract(int a, int b) {
return a - b;
}
static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(subtract(20, 10));
System.out.println(multiply(10, 20));
}
}Here every method has a different name:
add()
subtract()
multiply()
There is no method overloading.
Now:
class Calculator {
static int calculate(int a, int b) {
return a + b;
}
static int calculate(int a, int b, int c) {
return a + b + c;
}
static double calculate(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(calculate(10, 20));
System.out.println(calculate(10, 20, 30));
System.out.println(calculate(10.5, 20.5));
}
}Same method name:
calculate()but different parameter lists.
Therefore:
Method overloading.
Whenever you see a method, ask two questions:
Does the method take input?
Look at:
()or:
(int a, int b)Does the method give output?
Look at:
voidor:
int
double
StringThen you can immediately identify the method type.
static void display()Ask:
Arguments?
No.
Return?
No.
Therefore:
Type 1 — No Return + No Arguments.
static void display(int x)Arguments?
Yes.
Return?
No.
Therefore:
Type 2 — No Return + Arguments.
static int display()Arguments?
No.
Return?
Yes.
Therefore:
Type 3 — Return + No Arguments.
static int display(int x)Arguments?
Yes.
Return?
Yes.
Therefore:
Type 4 — Return + Arguments.
JAVA METHODS
|
-------------------------------
| |
PREDEFINED USER-DEFINED
METHODS METHODS
|
|
Depending on
arguments & return
|
-------------------
| | |
| | |
... ... ...
For user-defined methods:
METHOD
|
----------------------
| |
RETURN VALUE? ARGUMENTS?
| |
void / type () / values
The four combinations:
1. void method()
↓
No return + No arguments
2. void method(int x)
↓
No return + Arguments
3. int method()
↓
Return + No arguments
4. int method(int x)
↓
Return + Arguments
And finally:
SAME METHOD NAME
+
DIFFERENT PARAMETERS
↓
METHOD OVERLOADING
-
What is a method? → A reusable block of code that performs a specific task.
-
What are the two broad types? → Predefined and user-defined methods.
-
What are the four method types based on syntax? → No return/no arguments, no return/arguments, return/no arguments, return/arguments.
-
What is method overloading? → Same method name with different parameter lists.
-
Can
main()be overloaded? → Yes, but the JVM usespublic static void main(String[] args)as the standard entry point.