ESC
Type to search...
Dashboard / Java notes / Concepts / Basics / Part i

07 - Methods

P1 · Updated · Source
#java/basics #l0 #concept #must-know #status/known

Methods

What is it?

A method is a named block of code that you can call (run) from anywhere in your program by its name. Instead of writing the same logic in multiple places, you write it once in a method and call it whenever needed.

Why do methods exist?

  • Avoid repetition - write once, use many times
  • Organize code - break a big program into smaller, named, understandable pieces
  • Abstraction - you can call calculateAverage() without knowing how it works internally

Anatomy of a Method

public static void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

Breaking it down:

Part Meaning
public Visibility - accessible from outside the class
static Belongs to the class, not an instance (required in main-style programs for now)
void Return type - void means “this method returns nothing”
greet The method name - what you call it by
(String name) The parameter list - inputs the method accepts
{ ... } The body - the code that runs when the method is called

Methods with No Parameters, No Return Value

The simplest kind:

public static void sayHello() {
    System.out.println("Hello!");
}

// Calling it:
sayHello(); // Output: Hello!

Methods with Parameters

Parameters are the inputs you pass to a method:

public static void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

// Calling it:
greet("Alice"); // Output: Hello, Alice!
greet("Bob");   // Output: Hello, Bob!
  • "Alice" is the argument (the actual value you pass in)
  • name is the parameter (the variable inside the method that receives the value)

Note Parameter = the variable in the method definition. Argument = the value you pass when calling. People often use these terms interchangeably - that’s fine.

Multiple parameters

public static void printSum(int a, int b) {
    System.out.println(a + " + " + b + " = " + (a + b));
}

printSum(3, 5); // Output: 3 + 5 = 8

Methods that Return Values

Use a return type instead of void, and use the return keyword to send a value back:

public static int add(int a, int b) {
    return a + b;
}

// Calling it:
int result = add(3, 5);
System.out.println(result); // 8

Important Once return is hit, the method immediately stops - nothing after return in that method runs.

Return type must match

Return type What return should give back
void Nothing - no return needed (or just return;)
int An integer value
double A decimal value
String A string
boolean true or false
public static boolean isEven(int number) {
    return number % 2 == 0; // returns true or false
}

if (isEven(4)) {
    System.out.println("4 is even"); // prints this
}

How Method Calls Work (The Call Stack)

When you call a method, your current code pauses and waits for the method to finish. Then execution resumes from where it left off.

public static void main(String[] args) {
    System.out.println("Start");
    greet("Alice");              // pauses here, runs greet(), then comes back
    System.out.println("End");
}

public static void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

// Output:
// Start
// Hello, Alice!
// End

This stacking of method calls is called the call stack. The method currently running is always at the top.

Expressions as Arguments

You can pass any expression as an argument - Java evaluates it first, then passes the result:

greet(1 + 2); // Evaluates to greet(3)
add(x * 2, y - 1); // Both expressions evaluated before the call

Gotchas

  • A method with return type int MUST have a return statement that covers all paths - the compiler will error if any path returns nothing
  • Parameters are copies - changing a parameter inside a method does NOT change the original variable outside (for primitives). Objects behave differently
  • Method names are camelCase: calculateAverage, getUserInput - not CalculateAverage
  • You cannot call a non-static method from a static context without creating an object first (you’ll learn this when you get to classes)

Related: Objects, Condition, Loops, Printing