Think of 3LEVEL as learning the same topic in three depths:
- 🟢 LEVEL 1 → Beginner
- 🟡 LEVEL 2 → Intermediate
- 🔴 LEVEL 3 → Advanced / Interview
A method is a block of code designed to perform a particular task.
Example:
static void greet() {
System.out.println("Hello Java");
}Calling it:
greet();Output:
Hello Java
Create method → Call method → Method executes
Methods provide:
- Code reusability
- Modularity
- Readability
- Easy maintenance
- Avoiding repeated code
Instead of writing:
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");we can write:
static void greet() {
System.out.println("Hello");
}and call:
greet();
greet();
greet();Broadly:
Methods
|
├── Predefined Methods
|
└── User-defined Methods
Already provided by Java.
System.out.println("Hello");println() is a predefined method.
Created by the programmer.
static void greet() {
System.out.println("Hello");
}greet() is user-defined.
This is one of the most important classifications.
METHODS
|
---------------------------
| |
No Return Return
| |
-------- --------
| | | |
No Arg Arg No Arg Arg
void add() {
System.out.println(10 + 20);
}void add(int a, int b) {
System.out.println(a + b);
}int add() {
return 10 + 20;
}int add(int a, int b) {
return a + b;
}Arguments = Input Return = Output
General syntax:
modifier returnType methodName(parameters) {
// method body
}Example:
static int add(int a, int b) {
return a + b;
}Break it down:
static → modifier
int → return type
add → method name
int a, int b → parameters
return ... → method body
This is a very common confusion.
static int add(int a, int b)Here:
a and b → Parameters
When calling:
add(10, 20);Here:
10 and 20 → Arguments
Parameters → method declaration
Arguments → method call
static void hello() {
System.out.println("Hello");
}This creates the method.
hello();This executes the method.
Think:
Definition = Create the machine
Call = Start the machine
Suppose:
static int add() {
return 10 + 20;
}The int tells Java:
This method will return an integer value.
Then:
int result = add();The returned value is stored in result.
add()
↓
30
↓
result
void means the method doesn't return a value.
static void display() {
System.out.println("Hello");
}You cannot use it as an int result:
int x = display(); // ❌because display() returns nothing.
Now suppose we have:
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);
}Same name:
add
Different parameters:
(int, int)
(int, int, int)
This is:
Method Overloading
Method overloading is defining multiple methods with the same name but different parameter lists in the same class.
add(int a, int b)
add(int a, int b, int c)add(int a, int b)
add(double a, double b)show(int a, double b)
show(double a, int b)Changing only the return type is not enough.
❌ Invalid:
int add(int a, int b) {
return a + b;
}
double add(int a, int b) {
return a + b;
}The parameter lists are identical.
Therefore:
Return type alone cannot overload a method.
Consider:
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);
}
}Java needs to decide which show() to execute.
A useful three-step mental model is:
Method call
↓
1. Number of arguments
↓
2. Argument types
↓
3. Best matching method
For:
show(10);10 is an int.
Therefore:
show(int x)is the exact match.
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 these are two different methods:
main(String[] args)
main(int x)The JVM looks for the standard entry point:
public static void main(String[] args)It does not automatically start from:
main(int x)Therefore:
main()can be overloaded, but only the standardmain(String[] args)is the JVM entry point.
Methods can also be categorized based on whether they belong to the class or object.
static void display() {
System.out.println("Hello");
}Can be called directly from a static context:
display();void display() {
System.out.println("Hello");
}Normally requires an object:
Demo d = new Demo();
d.display();So:
Static method
↓
Class-level
Instance method
↓
Object-level
class Calculator {
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) {
int result1 = add(10, 20);
int result2 = add(10, 20, 30);
double result3 = add(10.5, 20.5);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}Here we have:
add(int, int)
↓
2 int arguments
add(int, int, int)
↓
3 int arguments
add(double, double)
↓
2 double arguments
Java selects the appropriate overloaded method based on the arguments.
METHODS IN JAVA
│
┌────────────┴────────────┐
│ │
PREDEFINED USER-DEFINED
│ │
Java-provided Programmer-created
│
┌────────┴────────┐
│ │
PARAMETERS RETURN VALUE
│ │
┌──────┴──────┐ ┌────┴────┐
│ │ │ │
NO YES NO YES
│ │ │ │
└──────┬──────┘ └────┬────┘
│ │
FOUR COMBINATIONS
│
┌───────────────┼────────────────┐
│ │ │
void m() void m(int) int m()
│
int m(int)
And:
Same method name
+
Different parameter list
↓
METHOD OVERLOADING
| Question | Answer |
|---|---|
| What is a method? | Reusable block of code performing a specific task |
| Why methods? | Reusability, modularity, readability, maintenance |
| Types? | Predefined and user-defined |
| Four syntax types? | Based on return type and arguments |
| What is a parameter? | Variable in method declaration |
| What is an argument? | Actual value passed during method call |
What does void mean? |
No return value |
| Can return type alone overload a method? | ❌ No |
| What is method overloading? | Same name + different parameter list |
Can main() be overloaded? |
✅ Yes |
Which main() is JVM entry point? |
public static void main(String[] args) |
| Arguments represent what? | Input |
| Return value represents what? | Output |