Introduction to Java Methods

A method in Java is a block of code that performs a specific task or operation. It is defined within a class and can be called (invoked) from other parts of the program. Methods allow you to encapsulate logic, making your code more modular and easier to understand.

Creating a Method

A method declaration includes:

  • returnType: The data type of the value returned by the method.
  • methodName: The name of the method followed by parantheses ().
  • parameterType: The data type of the parameter passed to the method.
  • parameterName: The name of the parameter used within the method.

For example, a method returnHello() with a return type of String:

public class DemoMethod {

  public String returnHello() {
    
    String retVal = "Hello";
    
    return retVal; // returns Hello
  }
}

Note: if the method doesn’t return a value then use void as the return type.

public class Printer {

  public void printName() {
    System.out.println("Hello");
  }
}

Note: there is no return statement when a method is declared as void.

Calling a Method

In java, methods are called by using the method name followed by parentheses:

methodName(); 

For example:

public class SimpleClass {

  public static void printHello() {
    System.out.println("Hello");
  }

  public static void main(String args[]) {
    printHello();
  }
}

In the above example, printHello() method is called from the main() method.

Parameters and Arguments

Parameters are variables that are used to pass information into methods.

public class Printer {

  public static void printName(String name) {
    System.out.println("Hello, " + name);
  }

  public static void main(String[] args) {
    myMethod("John"); // Outputs: Hello, John
  }
}

In the above method, the printName() takes one parameter of type String. When calling the above method, we have to pass in a variable of type String, e.g. “John”.

Methods can take also take in multiple parameters:

public class MethodDemo {

  public void multiParam(String name, int loop) {

    System.out.println("Print name x number of times");

    for(int i=0; i<=loop; i++) {
        System.out.println(name);
    }
  }
}

Return Values

A method can return a value, allowing you to use the result in your program.

public class Multiplier {

  public static int multiply(int x, int y) {
    return x * y;
  }

  public static void main(String[] args) {
    int result = multiply(5, 3);
    System.out.println("Result is: " + result); // Outputs: Result is: 15
  }
}

The multiply method’s return type is int, therefore it can only return int values.

Method Overloading

Method overloading allows you to have multiple methods with the same name but different parameters.

public class MethodDemo {

  public static int add(int x, int y) {
    return x + y;
  }

  public static double add(double x, double y) {
    return x + y;
  }

  public static void main(String args[]) {

    add(7, 4); //returns 11

    add(5.1, 4.9); // returns 10.0
  }
}

Conclusion

Java methods are essential building blocks that make our code organized, reusable, and maintainable. By understanding how to declare, call, and manipulate methods, you can write more efficient and clean code.

Happy Coding!