Java — Methods
What is a method?
A method is a named block of code that performs a specific task. Instead of writing the same logic repeatedly, you package it once and call it whenever needed:
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Ada"); // Hello, Ada!
greet("Grace"); // Hello, Grace!
}
Methods have two parts: definition (what they do) and invocation (calling them to run).
Anatomy of a method
static int add(int a, int b) {
int result = a + b;
return result;
}
| Part | Meaning |
|---|---|
static | Belongs to the class, not an instance (we'll cover instance methods with classes) |
int | Return type — the kind of value this method gives back |
add | Method name — follow camelCase convention |
int a, int b | Parameters — inputs the method expects |
return result | Sends the value back to the caller |
If a method doesn't return anything, use void as the return type.
Calling a method
int sum = add(3, 7);
System.out.println(sum); // 10
Java matches arguments to parameters by position. The first argument goes to the first parameter, and so on. The number of arguments must match the number of parameters.
Parameters vs arguments
People use these interchangeably, but technically:
- Parameters are the variables in the method definition:
int a, int b - Arguments are the actual values passed when calling:
3, 7
// a and b are PARAMETERS
static int add(int a, int b) { return a + b; }
// 3 and 7 are ARGUMENTS
int result = add(3, 7);
Return values
A method with a return type must use return:
static double calculateArea(double width, double height) {
return width * height;
}
The return statement sends a value back and exits the method immediately. Code after return never runs:
static int divide(int a, int b) {
if (b == 0) {
System.out.println("Cannot divide by zero");
return 0; // exit early
}
return a / b; // normal path
}
Methods with void return type can use return without a value to exit early:
static void printPositive(int number) {
if (number <= 0) return; // exit if not positive
System.out.println(number);
}
Method overloading
Java allows multiple methods with the same name but different parameter lists:
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
static String add(String a, String b) {
return a + b;
}
The compiler decides which version to call based on the argument types:
add(3, 7); // calls int version → 10
add(2.5, 3.1); // calls double version → 5.6
add("Hi ", "there"); // calls String version → "Hi there"
Overloading lets you provide convenient interfaces for the same concept. Java's println is overloaded — it accepts strings, numbers, booleans, and more.
Default parameters — Java doesn't have them
Unlike Python or JavaScript, Java doesn't support default parameter values. Instead, use method overloading:
static void greet(String name) {
greet(name, "Hello"); // calls the two-parameter version
}
static void greet(String name, String greeting) {
System.out.println(greeting + ", " + name + "!");
}
// Both work:
greet("Ada"); // Hello, Ada!
greet("Ada", "Hey"); // Hey, Ada!
Variable arguments (varargs)
When the number of arguments varies, use ...:
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
// Call with any number of arguments
System.out.println(sum(1, 2)); // 3
System.out.println(sum(1, 2, 3, 4)); // 10
System.out.println(sum()); // 0
varargs are treated as arrays inside the method. You can have at most one varargs parameter, and it must be the last one.
Static vs instance methods
So far all methods use static — they belong to the class and run without creating an object:
class Calculator {
static int add(int a, int b) {
return a + b;
}
}
// Call without creating an object
Calculator.add(3, 4);
Instance methods belong to objects. You need to create an instance first:
class Greeter {
String greeting;
Greeter(String greeting) {
this.greeting = greeting;
}
void greet(String name) {
System.out.println(greeting + ", " + name + "!");
}
}
Greeter hello = new Greeter("Hello");
hello.greet("Ada"); // Hello, Ada!
Instance methods can access instance variables — each object has its own copy.
Method scope
Variables created inside a method exist only during that method call:
static void doWork() {
int temp = 42; // exists only here
System.out.println(temp);
}
static void otherMethod() {
System.out.println(temp); // ERROR: temp doesn't exist here
}
Each method call creates a new stack frame with its own local variables. When the method returns, everything in that frame is destroyed.
Calling methods from methods
Methods can call other methods — this is how you build complex behavior from simple pieces:
static double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5.0 + 32;
}
static void printTemperature(double celsius) {
double fahrenheit = celsiusToFahrenheit(celsius);
System.out.printf("%.1f°C = %.1f°F%n", celsius, fahrenheit);
}
public static void main(String[] args) {
printTemperature(0); // 0.0°C = 32.0°F
printTemperature(100); // 100.0°C = 212.0°F
}
Each method does one thing. celsiusToFahrenheit converts. printTemperature formats and displays. Separation of concerns makes code testable and reusable.
Mini Practice
- Write a method that takes a string and returns it reversed
- Overload a method to accept either 2 or 3 integers and return their sum
- Write a method using varargs that finds the maximum value among any number of integers
- Create a method that converts miles to kilometers — call it from main
- Write a method that checks if a string is a palindrome — call it for several test cases
Next: scope — where variables live →
Related Topics
Frequently Asked Questions about Methods
What is Methods in Java?
Methods is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Methods?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Methods.
Why is Methods important in Java?
Methods is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.