SrcForge

Lesson 13

Methods in Java

Methods in Java are blocks of code that perform specific tasks when they are called. Methods help organize programs, reduce code repetition, and improve code readability and reusability.

Lesson content

What are Methods in Java?

Methods in Java are similar to functions in other programming languages. A method executes a set of statements whenever it is invoked or called.

Methods make programs modular by dividing large programs into smaller reusable parts.

Definition List

  • Method: A block of code that performs a specific task when called.
  • Parameter: A variable declared in a method definition that receives values during method calls.
  • Argument: The actual value passed to a method during execution.
  • Return Type: The data type of the value returned by a method.
  • Scope: The region in a program where a variable or method can be accessed.

Syntax of Methods in Java

Declaring a Method

Datatype methodname(){
  // block of code
}

Calling a Method

methodname()

Types of Methods in Java

Java methods are mainly divided into four types:

  1. Method with parameter without return type
  2. Method without parameter without return type
  3. Method with parameter with return type
  4. Method without parameter with return type

Parameters and Arguments in Java

Parameters and arguments are values passed to methods during program execution.

Example

add(50, 20);
  • 50 and 20 are arguments.
  • Variables receiving these values inside the method are parameters.

Java Scope

In Java, variables are accessible only within the region where they are created. This is known as scope.

Types of Scope

  • Local Scope
  • Method Scope
  • Class Scope

Static Method in Java

A static method belongs to the class rather than an object. Static methods can be accessed without creating an object of the class.

Example 01: Method with Parameter Without Return Type

This type of method accepts parameters but does not return any value.

package Methods;

public class method2 {
    public static void add(int a, int b) {
        int s = a;
        int p = b;
        int c = s + p;
        System.out.println(c);
    }

    public static void main(String[] args) {
        add(50, 20);
    }
}

Explanation

  • The add() method accepts two integer parameters.
  • It adds the values and prints the result.
  • The method does not return any value.

Example 02: Method with Parameter With Return Type

This method accepts parameters and returns a value.

package Methods;

public class method_3 {
    static int add(int c, int d) {
        return c + d;
    }

    public static void main(String args[]) {
        int m = add(20, 30);
        System.out.println(m);
    }
}

Explanation

  • The add() method returns the sum of two integers.
  • The returned value is stored in variable m.

Example 03: Different Types of Methods in Java

This example demonstrates multiple types of methods in a single program.

package Methods;

class Methods {
    // Method without parameter without return type
    public void add() {
        int a = 123;
        int b = 10;
        System.out.println("Addition : " + (a + b));
    }

    // Method with parameter without return type
    public void sub(int x, int y) {
        System.out.println("Subtraction : " + (x - y));
    }

    // Method without parameter with return type
    public int mul() {
        int a = 123;
        int b = 10;
        return a * b;
    }

    // Method with parameter with return type
    public float div(int x, int y) {
        return (x / y);
    }

    // Recursion Function
    public int factorial(int n) {
        if (n == 1)
            return 1;
        else
            return (n * factorial(n - 1));
    }
}

// Type of User Define Methods in Java
public class functions {
    public static void main(String args[]) {
        Methods o = new Methods();
        o.add();
        o.sub(123, 10);
        System.out.println("Multiplication : " + o.mul());
        System.out.println("Division : " + o.div(123, 10));
    }
}

Explanation of Method Types

Method Without Parameter Without Return Type

public void add()
  • Does not accept parameters
  • Does not return a value

Method With Parameter Without Return Type

public void sub(int x, int y)
  • Accepts parameters
  • Does not return a value

Method Without Parameter With Return Type

public int mul()
  • Does not accept parameters
  • Returns a value

Method With Parameter With Return Type

public float div(int x, int y)
  • Accepts parameters
  • Returns a value

Recursion in Java

Recursion is a process where a method calls itself repeatedly until a condition is satisfied.

Recursive Method Example

public int factorial(int n)

This method calculates the factorial of a number using recursion.

Advantages of Methods in Java

Code Reusability

Methods allow the same code to be used multiple times.

Better Code Organization

Programs become easier to read and maintain.

Reduces Complexity

Methods divide large programs into smaller manageable parts.

Easier Debugging

Errors can be identified and fixed more easily.

FAQ

1. What is a method in Java?

A method is a block of code that performs a specific task when called in a program.

2. What is the difference between parameters and arguments?

Parameters are variables in method definitions, while arguments are actual values passed during method calls.

3. What is recursion in Java?

Recursion is a technique where a method calls itself repeatedly until a stopping condition is met.

Methods are reusable blocks of code that perform specific tasks.
There are four main types of methods based on parameters and return types.
Parameters receive values during method calls, while arguments are the actual values passed.
Static methods belong to the class and can be called without creating an object.
Scope defines where variables and methods can be accessed in a program.
Recursion is when a method calls itself repeatedly until a base condition is met.
Methods improve code organization, reduce complexity, and enhance code reusability.

Where Java is used

Java in real-world development

Method Declaration

Syntax: DataType methodName() { //code }. Methods must have a return type and can accept parameters.

Parameters and Arguments

Parameters are variables in method definitions. Arguments are actual values passed when calling a method.

Return Types

Methods can return void (no value), primitive types (int, double, etc.), or reference types (String, objects).

Static Methods

Static methods belong to the class, not to objects. They can be called directly without instantiation.

Method Overloading

Multiple methods can have the same name if they have different parameters or parameter types.

Recursion

A method that calls itself repeatedly until a base condition is met. Useful for problems with recursive structures.