w1coding.comAll tutorials

Java Methods

Java lessons
  1. Environment Setup
  2. Introduction
  3. Variables
  4. Control
  5. Loops
  6. Arrays
  7. Methods
  8. OOP
  9. Contractors
  10. Inheritance
  11. Polymorphism
  12. Abtract
  13. Interfaces
  14. Constructors
  15. Static
  16. Exception
  17. Collections
  18. Lambda
  19. Streams
  20. Strings
  21. Date
  22. Interface
  23. Inheritance
  24. Polymorphism
  25. Exception
  26. File

Java Methods

Methods in Java are blocks of code that perform specific tasks. They allow for code reuse and can take inputs (parameters) and return results (return values).

Defining and Calling Methods

public class MethodExample {
    // Method that adds two numbers
    public static int addNumbers(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = addNumbers(5, 3);
        System.out.println("The sum is: " + result);
    }
}
    

This example defines a method addNumbers that takes two integers as parameters, adds them, and returns the result. The method is called in the main method.