Java Methods

Learn Java methods in detail including method declaration, parameters, return types, method overloading, and static methods.

What are Methods in Java?

Methods are blocks of code used to perform specific tasks.

Methods improve code reusability and readability.

Method Syntax

returnType methodName(){

    code;

}

Simple Method Example

public class Main {

    static void greet(){

        System.out.println("Welcome");

    }

    public static void main(String[] args) {

        greet();

    }

}

Method Parameters

static void showName(String name){

    System.out.println(name);

}

Return Type

static int add(int a, int b){

    return a + b;

}

Method Overloading

static int add(int a, int b){

    return a + b;

}

static double add(double a, double b){

    return a + b;

}

Static Methods

Static methods belong to class instead of objects.

Advantages