Skip to content

Methods – The Different Rooms in the House

Seanti edited this page Mar 24, 2025 · 1 revision

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.


What Are Methods?

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.


1. The main Method – The Entrance of the House

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.");
    }
}

Output:

You just entered the main room.

➡️ Java always starts at main—just like how you always enter a house through the main entrance.


2. Creating More Methods (Rooms)

We can create other methods inside our class. Each one will do something different.

Example: Different Rooms in the House

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!");
    }
}

Output:

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!


3. Methods with Parameters – Sending Information

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.

Example: Bringing Guests to the Kitchen

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.");
    }
}

Output:

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!


4. Methods with Return Values – Getting Something Back

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.

Example: Making Coffee in the Kitchen

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
    }
}

Output:

You just entered the main room.  
You are in the kitchen, making coffee.  
You received: A hot cup of coffee  

What Changed?

🚀 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 void method 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!

5. Why Use Methods?

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.


Final Takeaway: How Methods Work

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! 🎉

Clone this wiki locally