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.

Feb 22, 202624 views0 likes0 fires
18px

[!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!
}

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
1 questions5 min

Continue Learning

6. Arrays & The Enhanced For Loop

Beginner
9 min

7. User Input via Scanner

Beginner
8 min

8. Mathematical Operations & The Math Class

Beginner
7 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 Java Programming: From Zero to EnterpriseAll Categories