Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
1. Java Fundamentals
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

5. Methods (Functions) Architecture

Building reusable blocks of logic, passing parameters, and returning data.

Java Programming: From Zero to Enterprise
1. Java Fundamentals
February 22, 2026
100
A

[!NOTE] As your programs grow, putting all your code inside the main block becomes impossible to read and maintain. Methods (often called functions in other languages) allow you to break your code into small, testable, reusable blocks.

What is a Method?

A method is a block of code which only runs when it is explicitly called. You can pass data into a method (parameters), and it can return data back out (return values).

public class Main {
  // Method Definition
  static void printWelcomeMessage(String name) {
    System.out.println("Welcome to the system, " + name + "!");
  }

  public static void main(String[] args) {
    // Method Executions
    printWelcomeMessage("Alice"); // Outputs: Welcome to the system, Alice!
    printWelcomeMessage("Bob");   // Outputs: Welcome to the system, Bob!
  }
}

Let's dissect the method signature: static void printWelcomeMessage(String name)

  • static: Means this method belongs to the class itself, allowing us to call it directly without new Main().
  • void: This is the "Return Type". void means this method simply executes actions and returns absolutely nothing back to the caller.
  • String name: This is the parameter. It acts as a local variable inside the method.

Return Values

Methods become incredibly powerful when they calculate data and hand it back to the caller. To do this, change void to the data type you intend to return, and use the return keyword inside the block.

public class MathTools {
  // We declare that this method MUST return an 'int'
  static int addNumbers(int x, int y) {
    int sum = x + y;
    return sum; // Hand the data back!
  }

  public static void main(String[] args) {
    // The method executes, evaluates to '15', and assigns it to 'result'
    int result = addNumbers(5, 10);
    System.out.println("The answer is: " + result);
  }
}

[!IMPORTANT] The Return Exit: The moment a method hits a return statement, the method instantly terminates. Any code written beneath the return inside that method block will never be executed.

Pass by Value

In Java, method arguments are Passed by Value. This means Java creates a copy of your primitive variable and passes the copy into the method. If the method alters the parameter, your original variable remains totally unaffected!

static void tryToChange(int number) {
    number = 99; // Only changes the local copy!
}

public static void main(String[] args) {
    int myNum = 5;
    tryToChange(myNum);
    System.out.println(myNum); // Still outputs 5!
}

Designing Methods That Are Easy to Reuse

A good method should do one clear job. If a method prints a bill, calculates tax, saves to a database, and sends an email, it is doing too much. Smaller methods are easier to test and easier to understand.

From Messy Code to Clear Methods

// Instead of repeating discount logic everywhere:
static double applyDiscount(double price, double discountPercent) {
    double discount = price * discountPercent / 100;
    return price - discount;
}

static void printFinalPrice(String itemName, double price) {
    double finalPrice = applyDiscount(price, 10);
    System.out.println(itemName + ": " + finalPrice);
}

Notice how applyDiscount calculates and returns. It does not print. That makes it reusable in a console app, web app, or test case.

Method Naming Checklist

  • Use verbs: calculateTotal, printInvoice, isEligible.
  • Make boolean methods read like questions: isAdult, hasAccess.
  • Avoid vague names like process, handle, or doWork unless the context is already very clear.

Overloading in Practice

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

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

Overloading lets the same method name support different parameter lists. Java decides which method to call at compile time based on argument count and types.

Mini Practice

Write three methods: calculateArea for a rectangle, isPassing for marks, and formatName for first and last name. Keep each method focused on one responsibility.

Practice Lab: Refactor Into Methods

Take repeated logic and turn it into named reusable methods.

  1. Write a method calculateAverage(int a, int b, int c).
  2. Write a method isPassing(double average).
  3. Write a method printResult(String name, double average).
  4. Call all three from main.

Goal: Separate calculation, decision, and printing into different methods.

Revision Checkpoint

  • Method purpose: Package reusable logic behind a clear name.
  • Parameters: Inputs received by a method.
  • Return type: The type of value a method gives back.
  • void: Means the method returns no value.
  • Overloading: Same method name, different parameter list.

Before the quiz: Rewrite one long main method into three smaller methods with clear names.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

easyJava
Quiz: Methods
10 questions5 min
Lesson 5 of 10 in 1. Java Fundamentals
Previous in 1. Java Fundamentals
4. String Manipulation in Depth
Next in 1. Java Fundamentals
6. Arrays & The Enhanced For Loop
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories