Java Methods
Explanation • Examples • Real-Life
Projects • Practice Questions
What is a Method?
• A method is a block of code that performs a
task.
• Methods help in reusability and clean code.
• You must call a method to execute it.
Why Methods?
• Avoid repetition (DRY principle)
• Better organization
• Easier debugging
• Reusability
• Clean structure
Types of Methods
1. void methods (no return)
2. return-type methods
3. methods with parameters
4. methods without parameters
Example: void method
void sayHello() {
[Link]("Hello!");
}
Call: sayHello();
Example: method with return
• int add(int a, int b) {
• return a + b;
• }
• Call: int x = add(5, 10);
Real-Life Example: Making Tea
makeTea() method calls:
• boilWater()
• addTeaPowder()
• addMilk()
• addSugar()
• stirTea()
Mini Project: Shopping Bill
Methods:
• displayProducts()
• getPrice()
• generateBill()
Shows real-world usage.
Important Rules
• Method must be called
• Meaningful names
• Return only one value
• main() is also a method
Practice Questions (1–5)
1. Method to check even/odd
2. Method to find greatest of 3 numbers
3. Method for multiplication table
4. Method to count vowels in string
5. Method to sum array elements
Practice Questions (6–10)
6. Method to find max in array
7. Method to print star pattern
8. Method to calculate GST bill
9. Method to transpose 2D array
10. Menu-driven program using methods