05. Methods
A method is "a named bundle of code." Extracting repeated work into a method improves both readability and reuse. This lecture covers method declaration, overloading, the meaning of `static`, and varargs (`...`).
What you'll learn
- 1Know the method declaration form (modifier · return type · name · parameters)
- 2Distinguish overloading from overriding (overriding comes in lecture 08)
- 3Understand the difference between `static` and instance methods
- 4Use varargs (`String...`) safely
Overview
A method is "a named bundle of code." Extracting repeated work into a method improves both readability and reuse. This lecture covers method declaration, overloading, the meaning of `static`, and varargs (`...`).
Core Concepts
1) Method declaration
public static int add(int a, int b) {
return a + b;
}- Access modifier: `public` / `private` / (default) package-private / `protected`
- Return type: `void` if none
- Zero or more parameters
- Body: `{ ... }`
2) Overloading
static int max(int a, int b) { return a > b ? a : b; }
static double max(double a, double b) { return a > b ? a : b; }Same name, **different parameter signature** — you can have several methods that share a name.
3) `static` methods
class MathUtil {
static int square(int x) { return x * x; }
}
MathUtil.square(5); // called without an instance`static` methods belong to **the class itself** — call them as `ClassName.method()` without creating an object.
4) Varargs (`...`)
static int sumAll(int... nums) {
int s = 0;
for (int n : nums) s += n;
return s;
}
sumAll(1, 2, 3, 4); // 10
sumAll(); // 0Internally treated as an array. The varargs parameter must be the **last** one in the signature, and there can be only one.
Examples
Example 1 — `MethodBasics.java`: declare and call
public class MethodBasics {
public static void main(String[] args) {
int s = add(3, 4);
System.out.println("3 + 4 = " + s);
greet("Jisoo");
}
/** Sum of two integers. */
static int add(int a, int b) {
return a + b;
}
/** Print a greeting. */
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}**Output**
3 + 4 = 7
Hello, Jisoo!**Note:** `main` itself is a `static` method, after all.
Example 2 — `Overloading.java`: same name, different signature
public class Overloading {
public static void main(String[] args) {
System.out.println(max(3, 7));
System.out.println(max(3.5, 2.1));
System.out.println(max("apple", "banana"));
}
static int max(int a, int b) { return a > b ? a : b; }
static double max(double a, double b) { return a > b ? a : b; }
static String max(String a, String b) { return a.compareTo(b) > 0 ? a : b; }
}**Output**
7
3.5
banana**Note:** if only the return type differs, **overloading does not work**.
Example 3 — `StaticVsInstance.java`: class vs instance
public class StaticVsInstance {
static int staticCount; // one per class
int instanceCount; // one per instance
void increment() {
staticCount++;
instanceCount++;
}
public static void main(String[] args) {
StaticVsInstance a = new StaticVsInstance();
StaticVsInstance b = new StaticVsInstance();
a.increment(); a.increment();
b.increment();
System.out.println("a.instance=" + a.instanceCount);
System.out.println("b.instance=" + b.instanceCount);
System.out.println("static=" + StaticVsInstance.staticCount);
}
}**Output**
a.instance=2
b.instance=1
static=3**Note:** `static` fields are **shared across the class**, instance fields are **per-object**.
Example 4 — `Varargs.java`: variable arguments
public class Varargs {
public static void main(String[] args) {
System.out.println(sumAll());
System.out.println(sumAll(1, 2, 3));
System.out.println(sumAll(10, 20, 30, 40));
}
/** Sum any number of integers. */
static int sumAll(int... nums) {
int s = 0;
for (int n : nums) s += n;
return s;
}
}**Output**
0
6
100**Note:** `int...` behaves like an `int[]` under the hood.
Full example code (src/)
src/MethodBasics.java
public class MethodBasics {
public static void main(String[] args) {
int s = add(3, 4);
System.out.println("3 + 4 = " + s);
greet("Jisoo");
}
/** Sum of two integers. */
static int add(int a, int b) {
return a + b;
}
/** Print a greeting. */
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
src/Overloading.java
public class Overloading {
public static void main(String[] args) {
System.out.println(max(3, 7));
System.out.println(max(3.5, 2.1));
System.out.println(max("apple", "banana"));
}
static int max(int a, int b) { return a > b ? a : b; }
static double max(double a, double b) { return a > b ? a : b; }
static String max(String a, String b) { return a.compareTo(b) > 0 ? a : b; }
}
src/StaticVsInstance.java
public class StaticVsInstance {
static int staticCount;
int instanceCount;
void increment() {
staticCount++;
instanceCount++;
}
public static void main(String[] args) {
StaticVsInstance a = new StaticVsInstance();
StaticVsInstance b = new StaticVsInstance();
a.increment(); a.increment();
b.increment();
System.out.println("a.instance=" + a.instanceCount);
System.out.println("b.instance=" + b.instanceCount);
System.out.println("static=" + StaticVsInstance.staticCount);
}
}
src/Varargs.java
public class Varargs {
public static void main(String[] args) {
System.out.println(sumAll());
System.out.println(sumAll(1, 2, 3));
System.out.println(sumAll(10, 20, 30, 40));
}
/** Sum any number of integers. */
static int sumAll(int... nums) {
int s = 0;
for (int n : nums) s += n;
return s;
}
}
Common Mistakes
- Returning a value from a `void` method
- Using the same parameter name twice in a signature
- Accessing an instance field directly from a `static` method (not allowed)
- Confusing overloading with overriding — overriding belongs to inheritance (lecture 08)
- Putting varargs in the middle of the parameter list
Summary
- Methods are the basic unit of code reuse
- Same name, different signature → **overloading**
- `static` belongs to the class — call without an instance
- Varargs must be the last parameter, only one per method
Practice
# Practice - 05. Methods
## Exercise 1 — Prime check method
- File: `Homework01.java`
- Key concepts: method declaration, return value
Requirements
- Define `static boolean isPrime(int n)` — returns false if n < 2, otherwise check divisors.
- In main, print the result for n = 2..10.
Expected output
2 -> true
3 -> true
4 -> false
5 -> true
6 -> false
7 -> true
8 -> false
9 -> false
10 -> false## Exercise 2 — Average (overloading + varargs)
- File: `Homework02.java`
- Key concepts: overloading, varargs
Requirements
- Write `static double avg(int... nums)` and `static double avg(double... nums)`.
- Return 0 for empty input.
Expected output
int avg(1,2,3,4) = 2.5
double avg(1.5, 2.5) = 2.0
empty = 0.0## Solutions After trying it yourself, compare with [`answer/`](./answer/).
Solution code (homework/answer/)
answer/Homework01.java
/** Prime check. */
public class Homework01 {
public static void main(String[] args) {
for (int n = 2; n <= 10; n++) {
System.out.println(n + " -> " + isPrime(n));
}
}
static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
}
answer/Homework02.java
/** Average via overloading + varargs. */
public class Homework02 {
public static void main(String[] args) {
System.out.println("int avg(1,2,3,4) = " + avg(1, 2, 3, 4));
System.out.println("double avg(1.5, 2.5) = " + avg(1.5, 2.5));
System.out.println("empty = " + avg());
}
static double avg(int... nums) {
if (nums.length == 0) return 0;
int s = 0;
for (int n : nums) s += n;
return (double) s / nums.length;
}
static double avg(double... nums) {
if (nums.length == 0) return 0;
double s = 0;
for (double n : nums) s += n;
return s / nums.length;
}
}
Try It Yourself
cd 01_basics/05_methods/src
javac Overloading.java
java OverloadingNext Lecture
[06_Classes_and_Objects](../../02_객체지향/06_클래스와_객체/) — first step into OOP: defining a `class` and creating objects.
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗