Coding/Learn/java/methods
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnMethods in Java
25 / 31 0%
Lesson 25 of 31
6 mins read

Methods in Java

PreviousNext Lesson
In Plain English

A method is a block of code containing a series of statements that runs only when it is called. Methods allow you to reuse code without repeating yourself (DRY principle), breakdown complex logic into manageable chunks, and return calculated results.

The Mental Model

"A method is like a vending machine button: you press the "Dispense Water" button (call method), the machine executes its internal steps, and drops a bottle into the tray (returns value)."

Deep Dive: How It Works

Method Signature: `public static returnType methodName(parameters) { ... }`

The `void` keyword: Specifies that the method performs an action but does not return a value back.

The `return` keyword: Passes a value back to the caller and terminates method execution.

Why `static`? A static method belongs to the class and can be invoked directly from `main` without creating an object instance.

Syntax Blueprint

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

Define return type before method name. Use return keyword to send value back.

PointCore Rules to Remember

Point star
Return Types: Can return primitives (int, double, boolean), objects (String), or void.
Point star
Static Context: static methods can be called directly from other static methods like main().
Point star
DRY Principle: Write once in a method, call hundreds of times.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting return statement in non-void method

Why it happens: If method returns int or String, every code branch must end in a return statement.

How to fix: Ensure all execution paths return a value matching the declared type.

Pro Tip: Keep methods focused on doing a single specific job well (Single Responsibility Principle).

Live Interactive Example

Hit Run Code to see it live
Defining and Calling Methods
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: calculateTax returns 8.5, which main adds to subtotal to produce 108.5.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a square number method

  • PointComplete the static method "square" that takes int n and returns n * n.
  • PointIn main, call square(6) and print "Result: " + square(6).
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates