Java Methods

Understand how to define, call, and overload methods to create reusable code.

Methods (also called functions) allow you to write reusable and modular code in Java. They help organize logic, avoid repetition, and improve maintainability.

1. Declaring and Calling Methods

A method is defined once and can be called multiple times. A basic method includes:

  • Return type (or void if nothing is returned)
  • Method name
  • Optional parameters
  • Method body

Try example:

public class MethodDemo {

    static void greet() {
        System.out.println("Welcome to VINAR TECH Tutorials");
    }

    public static void main(String[] args) {
        greet();  // Calling the method
        greet();  // You can call it multiple times
    }
}

2. Parameters and Return Types

You can pass values to a method (known as parameters). Methods may also return a value.

Try example:

public class ParameterDemo {

    static int add(int a, int b) {
        return a + b;  // Method returns an integer value
    }

    public static void main(String[] args) {

        int result = add(10, 20);
        System.out.println("Sum = " + result);
    }
}
Types of Methods
  • Void methods: Perform an action but return nothing.
  • Non-void methods: Return a computed value.
  • Static methods: Called without creating an object.
  • Instance methods: Require an object of the class.

3. Method Overloading

Method overloading means defining multiple methods with the same name but different parameter lists.

Useful when performing similar operations with different input types.

Try example:

public class OverloadDemo {

    static int sum(int a, int b) {
        return a + b;
    }

    static double sum(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {

        System.out.println(sum(5, 10));       // int version
        System.out.println(sum(5.5, 2.7));    // double version
    }
}

4. Returning Objects from Methods

Methods can return not only primitive values but also objects.

Try example:

class User {
    String name;

    User(String name) {
        this.name = name;
    }
}

public class ObjectReturnDemo {

    static User createUser(String name) {
        return new User(name);
    }

    public static void main(String[] args) {
        User u = createUser("VINAR TECH");
        System.out.println("Created User: " + u.name);
    }
}

5. Recursion

Recursion occurs when a method calls itself. Every recursive method must have:

  • A base condition (stopping point)
  • A recursive step (calls itself with new input)

Try example:

public class RecursionDemo {

    static int factorial(int n) {

        if (n == 0) {
            return 1;       // Base case
        }

        return n * factorial(n - 1);  // Recursive call
    }

    public static void main(String[] args) {

        System.out.println("Factorial of 5 = " + factorial(5));
    }
}

6. Best Practices for Methods

  • Use meaningful method names (e.g., calculateTotal())
  • Keep methods short and focused on a single task
  • Organize related methods inside proper classes
  • Use method overloading to avoid confusing method names
  • Use recursion only when it simplifies logic

7. Static vs Instance Methods

In Java, methods are classified as static or instance methods based on how they are accessed. Understanding this difference is critical for writing clean and correct object-oriented programs.

A static method belongs to the class itself and can be called without creating an object. It is commonly used for utility functions and entry points like main().

Try example:

public class MathUtils {

    static int square(int n) {
        return n * n;
    }

    public static void main(String[] args) {
        System.out.println(square(5));
    }
}

An instance method belongs to an object and requires an instance of the class to be called. These methods usually operate on object data.

Try example:

class Calculator {

    int multiply(int a, int b) {
        return a * b;
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.multiply(3, 4));
    }
}

Tip: Use static methods for shared logic and instance methods when behavior depends on object state.

8. Call by Value in Java

Java always uses call by value. This means a copy of the variable is passed to the method, not the original variable itself.

For primitive data types, changes inside the method do not affect the original value.

Try example:

public class CallByValueDemo {

    static void change(int x) {
        x = 100;
    }

    public static void main(String[] args) {
        int num = 50;
        change(num);
        System.out.println(num); // Output: 50
    }
}

When objects are passed, the reference is copied, so changes to object data are visible outside the method.

9. Real-World Method Design Tips

  • A method should perform one clear responsibility
  • Avoid very long parameter lists
  • Return meaningful values instead of printing inside methods
  • Reuse methods instead of duplicating logic
  • Name methods using verbs (e.g., calculateTax())

Well-designed methods improve readability, testing, and long-term maintenance of Java applications.

10. Common Mistakes While Using Methods

Beginners often misuse methods due to misunderstanding scope, return types, or static usage. Avoiding these mistakes early helps in writing clean and bug-free Java programs.

  • Forgetting to return a value from non-void methods
  • Calling instance methods without creating an object
  • Using static unnecessarily everywhere
  • Writing very large methods instead of smaller reusable ones
  • Ignoring meaningful method names

Always test methods independently and keep logic simple. As applications grow, properly designed methods make debugging and maintenance much easier.