Skip to main content

Section 3.3 Method Parameters

Remember how methods helped us avoid copying and pasting code? That was great! But as our programs grow bigger, we’re running into a new problem: too many separate variables that really belong together.

Subsection 3.3.1 The Problem: Too Many Separate Pieces

Think of it like packing for school. Instead of one backpack, imagine carrying:
Sure, you could do it, but it’s awkward and things might fall! This is exactly what’s happening in our game code:
int playerHealth  = 100;
int playerGold    = 50;
int playerXp      = 0;
int playerMana    = 75;
int playerStamina = 90;
All these variables describe one thing (the player), but we’re carrying them separately. This leads to clumsy method calls:
// We have to pass everything one by one
public static int increaseHealth(int currentHealth, int healAmount) {
    return currentHealth + healAmount;
}

public static int increaseGold(int currentGold, int goldReward) {
    return currentGold + goldReward;
}
Using them gets tedious:
playerHealth = increaseHealth(playerHealth, 5);
playerGold   = increaseGold(playerGold, 10);

Subsection 3.3.2 Why This Is A Problem

Here’s why juggling so many separate variables causes trouble:
  1. What if we need a method that changes both health and gold? We’d need something like:
    public static void awardVictory(int health, int gold, int xp, int mana /* ... */) {
        // ...
    }
    
    That’s too many parameters and a lot to keep track of!
  2. The compiler can’t help us if we mix up the order:
    // Oops! Accidentally swapped health and gold
    awardVictory(playerGold, playerHealth);  // This will compile! 😱
    
    It compiles, but it’s obviously wrong—Java doesn’t know they’re reversed. These are easy mistakes to make!
  3. It gets worse with two players:
    int player1Health = 100;
    int player1Gold   = 50;
    
    int player2Health = 100;
    int player2Gold   = 50;
    
    // ... this is getting out of hand!
    
    What if we had 4, 5 or even 50 players!

Subsection 3.3.4 What’s Coming Next?

In our next section, we’ll learn:
  • How to create our own classes (like that Player example)
  • How to keep data safe inside our classes
  • How to write methods that belong to our classes

Subsection 3.3.5 Exercises: Method Parameters

Checkpoint 3.3.1. Multiple Choice: Why Too Many Separate Pieces Is a Problem.

In the "Method Parameters" section, we compared carrying many separate school items to storing many separate player stats. Which statement below best captures why this approach can cause issues?
  • Because Java forbids having more than three int variables in a class.
  • Java doesn’t have a hard limit on the number of int variables; the issue is about code organization.
  • It becomes easy to mix up or misuse parameters, and methods end up with too many arguments to manage safely.
  • Correct. Too many separate variables means large parameter lists and accidental ordering mistakes.
  • We can’t compile if we have more than two parameters in a method header.
  • Not true; Java methods can have many parameters, but it’s still risky or unwieldy to do so.
  • Passing multiple int variables will permanently corrupt memory in Java.
  • Java won’t corrupt memory. The real danger is logical mistakes with too many separate pieces.

Checkpoint 3.3.2. True/False: Swapped Parameters.

    If two method parameters have the same type, swapping them by accident (e.g., passing playerGold where playerHealth is expected) will still compile and could lead to unexpected behavior at runtime.
  • True.

  • The compiler won’t stop you if the swapped parameters are both the same type, making it a silent but dangerous error.
  • False.

  • The compiler won’t stop you if the swapped parameters are both the same type, making it a silent but dangerous error.

Checkpoint 3.3.3. Short Answer: Handling Multiple Players.

In the section, we saw how having player1Health, player2Health, player1Gold, player2Gold, and so on can get “out of hand.” In 1-2 sentences, explain why managing multiple players with separate variables becomes difficult. Feel free to reference the “backpack” analogy.
Solution.
Sample Solution: Each player has several stats to track, so using a separate variable for each stat (like player1Health, player2Health, etc.) quickly leads to long parameter lists and confusion. Bundling them into a single structure (a “backpack” or a Player class) simplifies both method calls and data organization.

Checkpoint 3.3.4. Multiple Choice: The transferMoney Example.

Consider the method signature public static void transferMoney(int fromBalance, int toBalance, int amount). Which of the following best describes the biggest risk with this approach?
  • Mixing up fromBalance and toBalance is easy to do, and the compiler won’t catch it.
  • Precisely. Since all parameters are int, swapping them won’t trigger a compile error.
  • The code will always trigger a compile-time error if someone calls the function with the wrong order.
  • That’s incorrect; it will compile unless types differ.
  • It’s impossible to pass three parameters to a void method in Java.
  • Not true. Java allows as many parameters as you want.
  • If the method is static, it can’t accept multiple parameters.
  • A static method can still have multiple parameters. This is not the main concern.

Checkpoint 3.3.5. Reflection: Preview of Classes.

We saw a hint of a Player class that might hold health, gold, xp, etc. In 2-3 sentences, describe why bundling these stats into a single object could make code easier to maintain, before we even learn the full syntax of classes.
Solution.
Sample Solution: One object can store all the player’s related data, so we don’t have to pass a dozen parameters whenever we modify or read stats. It also reduces the risk of mixing up parameter order. Once we have an object, we can define methods like p1.increaseHealth(5) that automatically update the right fields, making the code more organized and less error-prone.
You have attempted of activities on this page.