How to Create Methods in Java - Creating and Calling Functions

How to Create Methods in Java – Creating and Calling Functions

Methods in Java: Creating and Calling Functions

Methods (also called functions) are a fundamental building block in Java programming. They help organize code into reusable blocks, making it more readable, maintainable, and efficient. In this guide, we’ll explore how to create and call methods in Java, along with best practices and real-world examples.

What are Methods in Java?

A method in Java is a block of code designed to perform a specific task. Methods allow developers to:

  • Encapsulate functionality and reuse code
  • Improve readability and maintainability
  • Make programs more modular

Every Java method consists of:

  • Method Signature (method name and parameters)
  • Return Type (data type returned by the method, if any)
  • Method Body (the logic to be executed)

📖 Learn more: Oracle Docs – Java Methods


How to Create a Method in Java

To define a method, use the following syntax:

returnType methodName(parameters) {
    // Method body
    return value; // Optional (only for non-void methods)
}

Example: A Simple Java Method

public class Example {
    // Method that returns a greeting message
    public static String greet(String name) {
        return "Hello, " + name + "!";
    }
    
    public static void main(String[] args) {
        String message = greet("Alice");
        System.out.println(message);
    }
}

Types of Methods in Java

Java provides several types of methods:

1. Void Methods (Methods that Do Not Return a Value)

public static void printMessage() {
    System.out.println("This is a message!");
}

2. Methods with Return Values

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

📖 Learn more: Java Return Types

3. Parameterized Methods (Methods with Arguments)

public static void displayMessage(String message) {
    System.out.println(message);
}

4. Method Overloading (Multiple Methods with the Same Name)

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

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

public static double multiply(double a, double b) {
    return a * b;
}

Calling Methods in Java

Once a method is defined, it can be called (invoked) from another part of the program.

Calling Static Methods

Static methods belong to the class and can be called without creating an instance.

public class MathUtils {
    public static int square(int num) {
        return num * num;
    }
    
    public static void main(String[] args) {
        int result = square(5);
        System.out.println("Square: " + result);
    }
}

Calling Instance Methods

Instance methods require an object of the class to be called.

public class Person {
    String name;
    
    public void introduce() {
        System.out.println("Hello, my name is " + name);
    }
    
    public static void main(String[] args) {
        Person person = new Person();
        person.name = "Alice";
        person.introduce();
    }
}

Frequently Asked Questions (FAQ)

1. What is the difference between a function and a method in Java?

In Java, all functions belong to a class and are called methods.

2. Can a method return multiple values?

A method can return an object, an array, or use a wrapper class to return multiple values.

3. What is method overloading in Java?

Method overloading allows multiple methods with the same name but different parameters within a class.

4. What is the difference between static and instance methods?

  • Static methods belong to the class and do not require an instance.
  • Instance methods belong to an object and require an instance to be invoked.

5. How do I call a method from another class?

Use an instance of the class to call its methods, or call static methods using the class name.

public class Utils {
    public static void sayHello() {
        System.out.println("Hello!");
    }
}

public class Main {
    public static void main(String[] args) {
        Utils.sayHello();
    }
}

Conclusion

Understanding how to create and call methods is essential for writing clean, reusable Java code. Mastering methods allows for better code organization and modular programming.

🚀 Now that you understand methods, let’s talk about one of the most important concepts in Java: Object-Oriented Programming!

Scroll to Top