Think of methods like your favorite recipes - write them once, use them many times! Just as you wouldnโt want to write down a cookie recipe every time you bake, Java methods let us write code once and reuse it wherever needed.
Note: You might hear these called "functions" in other languages like Python or JavaScript. In Java, we typically say "methods" when the code belongs to a class.
public class IntroMethods1 {
public static void main(String[] args) {
int playerHealth = 10;
int playerGold = 50;
// First victory
playerHealth += 5;
playerGold += 10;
// Second victory
playerHealth += 5;
playerGold += 10;
System.out.println("Health: " + playerHealth);
System.out.println("Gold: " + playerGold);
}
}
Hold up! Did you notice we wrote the same code twice? Just like copying homework isnโt the best idea, copying code can lead to problems. What if we need to change how much gold a player gets? Weโd have to find and update every copy!
static - Means this method belongs to the class itself, not to any specific instance. Weโll explore this more when we learn about objects and classes.
public static int addTwoNumbers(String a, int b) { ... }
the compiler flags an incompatible type error when you try addTwoNumbers(3, 5). It expects (String, int), not (int, int). The compiler is your "early alert system" for mismatched parameter types.
public static void awardVictory(int health, int gold) {
// health is now a COPY of playerHealth (10)
health += 5; // health becomes 15, but only inside this method
// gold is a COPY of playerGold (50)
gold += 10; // gold becomes 60, but only inside this method
// When method ends, copies are discarded!
}
// In main:
playerHealth = 10; // Original stays 10
playerGold = 50; // Original stays 50
Think about it: If you make a photocopy of a document and write on the copy, does it change the original? Thatโs what Java does with int parametersโit makes copies!
Now, each method returns the updated int, and we assign it back into playerHealth or playerGold. If you wanted "gold +12" instead of "+10," youโd edit increaseGold() in one place.
The compiler canโt save us hereโit sees (int, int) and thinks "looks good!" even if weโve mixed up which int is which. There must be a better way...
But we still juggle many separate variables. The next step? "Bundle them in one container." In Java, that container is a class. Then we can do player1.heal(5) without passing health1, xp1, gold1.
Coming Up Next: Weโll learn about classes - a way to keep related data and methods together, like keeping all your school supplies in one backpack instead of carrying them separately!
Hereโs some starter code to get you started. To prevent compiler errors, weโve included a return statement with a valid data type, though the value itself may be just a placeholder. Be sure to update the return value accordingly as you implement the actual logic.
Checkpoint3.2.1.Multiple Choice: The Repeated Code Problem.
In the example where we repeatedly wrote playerHealth += 5; playerGold += 10; for each victory, which of the following best explains why that repeated code is problematic?
Checkpoint3.2.4.Short Answer: Why Did the void Method Fail to Update Player Stats?
In the example, we tried public static void awardVictory(int health, int gold) hoping it would directly update playerHealth and playerGold, but it didnโt work as intended. In 2-3 sentences, explain why void methods didnโt update the original variables, and how returning a new value instead solves the problem.
Sample Solution: Java passes health and gold as copies, so changing them in the void method does not affect the original playerHealth or playerGold. By returning the updated values (e.g., return currentHealth + 5;), we can reassign playerHealth to that new value in the main method, ensuring the original variable changes.
If you forget these rules, or pass arguments of the wrong type, your compiler is your first line of defenseโhelping you spot errors early, in true design-recipe spirit.