-
Notifications
You must be signed in to change notification settings - Fork 0
Methods – The Different Rooms in the House
We already talked about how a class is like a house, and methods are like the rooms inside.
Each room (method) has a specific purpose, just like how a kitchen is for cooking and a bedroom is for sleeping.
A method is a block of code that does a specific task. Instead of writing the same code over and over, we put it inside a method and just "call" the method when needed.
Think of it as a room in the house—you only enter it when you need something from that room.
Java always starts in the main method. It's like the entrance of the house.
public class MyHouse {
public static void main(String[] args) {
System.out.println("You just entered the main room.");
}
}You just entered the main room.
➡️ Java always starts at main—just like how you always enter a house through the main entrance.
We can create other methods inside our class. Each one will do something different.
public class MyHouse {
public static void main(String[] args) {
System.out.println("You just entered the main room.");
kitchen(); // Go to the kitchen
bedroom(); // Go to the bedroom
}
public static void kitchen() {
System.out.println("You are now in the kitchen. Time to eat!");
}
public static void bedroom() {
System.out.println("You are now in the bedroom. Time to sleep!");
}
}You just entered the main room.
You are now in the kitchen. Time to eat!
You are now in the bedroom. Time to sleep!
➡️ Java starts in main (the entrance).
➡️ Then, it moves to kitchen() (a room).
➡️ Then, it moves to bedroom() (another room).
🔹 Each method does only one task, keeping things organized!
Sometimes, when you enter a room, you bring something with you.
For example, when you go to the kitchen, you may bring the number of guests you're cooking for.
We use parameters to send information into a method.
public class MyHouse {
public static void main(String[] args) {
System.out.println("You just entered the main room.");
kitchen(3); // Cooking for 3 guests
}
public static void kitchen(int guests) {
System.out.println("You are in the kitchen, cooking for " + guests + " guests.");
}
}You just entered the main room.
You are in the kitchen, cooking for 3 guests.
➡️ We passed 3 into the kitchen() method, and it used that number!
Sometimes, when you go to a room, you take something with you when you leave.
For example, if you go to the kitchen, you may leave with a plate of food.
A method that returns a value works the same way—it gives something back when it finishes.
public class MyHouse {
public static void main(String[] args) {
System.out.println("You just entered the main room.");
String coffee = kitchen(); // Get coffee from the kitchen
System.out.println("You received: " + coffee);
}
public static String kitchen() { // Notice we changed 'void' to 'String'!
System.out.println("You are in the kitchen, making coffee.");
return "A hot cup of coffee"; // This method now RETURNS a string
}
}You just entered the main room.
You are in the kitchen, making coffee.
You received: A hot cup of coffee
🚀 We changed void to String in the kitchen() method.
➡️ void means the method does something but doesn’t return anything.
➡️ Now, because we changed it to String, it MUST return a string!
➡️ The method returns "A hot cup of coffee", and we store it in coffee.
📌 Think of it like this:
-
A
voidmethod is like turning on the lights—it does something, but you don’t take anything with you. - A method with a return value is like going to the kitchen and coming back with a cup of coffee!
✅ Organized Code – Splitting tasks into methods keeps your program clean and readable.
✅ Reusability – You can call a method multiple times without rewriting code.
✅ Easy to Manage – If you need to fix or update a task, you only change one method instead of multiple places in the code.
| Method Type | Think of it like... | Example |
|---|---|---|
Regular Method (void) |
A room in the house | public static void kitchen() {...} |
| Method with Parameters | Bringing something into a room | public static void kitchen(int guests) {...} |
| Method with Return Value | Taking something out of a room | public static String kitchen() {... return "Coffee"; } |
That’s it for methods! 🎉