Java Tutorial

Methods

Declaration, parameters, return types, and overloading

What are Java Methods?

In Java, a method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions in other programming languages.
java
public class Calculator {
    // A simple method!
    public int add(int a, int b) {
        return a + b;
    }
}

Method Components

A method has several parts that the compiler uses to understand how it should work:
  1. Access Modifier: Who can call the method? (public, private, protected).
  2. Return Type: What data does it send back? (int, String, void for nothing).
  3. Name: The name used to call it. (In camelCase!).
  4. Parameters: The data passed into the method inside ().
  5. Body: The actual code inside {}.
java
// public access, returns nothing (void)!
public void greet(String name) {
    System.out.println("Hello, " + name);
}

Method Overloading

In Java, multiple methods can have the SAME name, as long as they have DIFFERENT parameters (different types or different number of inputs). This is called Method Overloading.
java
public int sum(int a, int b) { return a + b; }
public double sum(double a, double b) { return a + b; }
public int sum(int a, int b, int c) { return a + b + c; }
Wait! Java uses the arguments you pass to automatically figure out which version of the method to run.

Static Methods vs. Instance Methods

  • Static Method: Belongs to the class itself. You can call it WITHOUT creating an object! (Math.sqrt()).
  • Instance Method: Belongs to an object. You must create an object with new before you can call it!
java
public class Helper {
    public static void log(String msg) {
        System.out.println("Log: " + msg);
    }
}

Helper.log("Application started!"); // No 'new' needed!

Summary

TermDescription
MethodA block of code to perform an action
ParameterVariable defined in the method signature
ArgumentThe ACTUAL data passed into the method
ReturnKeyword to send data back to caller
voidReturn type for methods that send NOTHING back
OverloadingMultiple methods with same name; different inputs
StaticBelongs to class; no object needed
FinalMethod cannot be "overridden" by a child class
Best ForCode reuse, abstraction, organizing logic
camelCaseJava convention for names (e.g., calculateResult)
SignatureThe method name + its parameter types
Varargs(int... nums) (Variable number of arguments!)
RecursionA method that calls itself (Use carefully!)
thisRefers to the current object inside the method
Throwsvoid f() throws Exception (Declaring errors!)
ScopeVariables inside a method only exist there!
ParameterPass by value (Primitives) vs. Pass by reference (Objects)
InterfaceMethods without a body (See Interfaces section!)
ConstructorA special method to create objects!
Getter/SetterMethods used to access private data safely
FunctionalUsing "Lambda" to pass methods as data (Java 8+)
JitThe JVM can "inline" small methods to make them faster
StackEach method call gets its own space on the call stack
NestingYou cannot define a method INSIDE another method!