Methods
Right now, all your code lives inside main() . That works for small programs, but imagine a 500-line main() method — impossible to read, debug, or reuse! Methods let you split your code into small, named, reusable pieces. Think of them like recipe cards: each method does one thing, and you call it by name whenever you need it.
Learn Methods in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
💡 Real-World Analogy: Recipe Cards
A method is like a recipe card. It has a name ("Chocolate Cake"), ingredients (parameters — flour, sugar, eggs), instructions (the method body), and a result (return value — a finished cake). You write the recipe once, and you can follow it any time without memorizing the steps — just call the recipe by name.
Every method has a specific structure. Let's break it down piece by piece:
🔑 Key insight: You define a method once (write the recipe), then call it as many times as you want (follow the recipe). The code inside the method only runs when you call it — defining it doesn't execute it.
Most methods need some data to work with. Parameters are variables listed in the method definition that receive values when the method is called. The values you pass in are called arguments .
Think of it like a form: the parameter is the blank field label ("Name: ___"), and the argument is what you fill in ("Alice").
A void method does something but doesn't give you anything back (like printing). A return method computes a value and hands it back to whoever called it. You can then store that value, print it, or use it in another calculation.
Java allows you to have multiple methods with the same name , as long as they have different parameter lists . The compiler picks the right version based on the arguments you pass. This is called overloading .
Real-world example: System.out.println() is overloaded! It can print a String, int, double, boolean, char — there are actually 10+ versions of println() , and Java picks the right one based on what you pass.
This concept confuses many beginners, so pay close attention. Java is always pass-by-value . This means when you pass a variable to a method, the method gets a copy of the value, not the original variable. Changes inside the method don't affect the original.
🔑 The workaround: If you need a method to produce a new value, use return :
Variables declared inside a method are local to that method — they don't exist outside it. This is called scope . Each method is like its own room: you can't see what's inside another room.
💡 This is a feature, not a bug! Scope prevents methods from accidentally interfering with each other. You can use the same variable name ( result , i , temp ) in different methods without any conflict.
You've been using static methods so far because they work without creating objects. Once you learn OOP (Lesson 9), you'll use instance methods too. Here's a quick preview:
Call directly on the class. No object needed.
For now, keep using static for all your methods. You'll understand the difference deeply in the OOP lesson.
💡 Single Responsibility: Each method should do ONE thing and do it well. calculateTax() — yes. calculateTaxAndPrintAndSaveToFile() — no!
💡 Descriptive names with verbs: calculateShippingCost() , isValid() , getUserName() — not calc() , check() , get() .
💡 Keep methods small: Ideally 5-15 lines. Maximum 20-30 lines. If it's longer, break it up.
💡 Limit parameters: 0-3 parameters is ideal. If you need 4+, consider grouping them into an object.
💡 No surprises: A method named getTotal() should only return a total — it shouldn't also print something or save to a file.
You now know how to write clean, reusable methods with parameters, return values, overloading, and proper scope! Methods are the foundation of organized Java code — every professional program is built from hundreds of small, focused methods.
Next up: Arrays — store and access collections of data to process lists, tables, and datasets efficiently.
Practice quiz
What does the return type 'void' mean for a method?
- It returns an int
- It cannot be called
- It returns nothing
- It returns a String
Answer: It returns nothing. A void method performs an action but does not return a value.
What is the difference between a parameter and an argument?
- A parameter is the variable in the definition; an argument is the value passed in
- They are the same thing
- An argument is in the definition; a parameter is passed in
- Parameters are only for void methods
Answer: A parameter is the variable in the definition; an argument is the value passed in. Parameters are named in the method definition; arguments are the actual values you pass when calling.
What is method overloading?
- Calling a method too many times
- A method that returns multiple values
- A method that is too long
- Multiple methods with the same name but different parameter lists
Answer: Multiple methods with the same name but different parameter lists. Overloading means several methods share a name but differ in their parameter lists.
Can you overload two methods that differ ONLY in their return type?
- Yes, always
- No — the parameter lists must differ
- Only for void methods
- Only with static
Answer: No — the parameter lists must differ. You cannot overload by return type alone; the parameter lists must differ.
Java passes arguments by value. What does that mean for a primitive like int?
- The method gets a copy, so the original is unchanged
- The method can change the original variable
- The original becomes null
- It throws an exception
Answer: The method gets a copy, so the original is unchanged. Java is pass-by-value: the method receives a copy of the value, so changes inside don't affect the original.
What does this print? static void d(int x){x*=2;} int num=10; d(num); System.out.println(num);
- 20
- 0
- 10
- An error
Answer: 10. d gets a copy of num; doubling the copy leaves the original num as 10.
What is variable scope in the context of methods?
- How fast a method runs
- Variables declared in a method are local and don't exist outside it
- The number of parameters
- The return type
Answer: Variables declared in a method are local and don't exist outside it. Variables declared inside a method are local to it — they cannot be seen from other methods.
Given 'static int add(int a, int b){return a+b;}', what does add(multiply(3,4), 5) return if multiply(3,4) is 12?
- 12
- 60
- 20
- 17
Answer: 17. multiply(3,4) is 12, then add(12, 5) returns 17.
How do you correctly use a method's returned value?
- add(5, 3); on its own line
- int result = add(5, 3);
- void result = add(5, 3);
- return add(5, 3);
Answer: int result = add(5, 3);. Store the result in a variable: int result = add(5, 3); calling it alone discards the value.
Which of these is a static method call that needs no object?
- name.length()
- name.toUpperCase()
- Math.sqrt(16)
- account.getBalance()
Answer: Math.sqrt(16). Math.sqrt is static — called on the class directly; the others are instance methods needing an object.