Sobes.tech
Junior — Middle

Could you explain what is meant by defining a method in programming?

sobes.tech AI

Answer from AI

In programming, a method is a named block of code that performs a specific task and can be called from other parts of the program. In Java, a method is a block of code associated with a class or object that can accept parameters and return a result.

Example of a method in Java:

public class Calculator {
    // Method for adding two numbers
    public int add(int a, int b) {
        return a + b;
    }
}

Here, add is a method that takes two integers and returns their sum. Methods help structure code, make it reusable, and easier to maintain.