Java Programming Handbook

    Methods in Java

    In Java, methods are blocks of code that perform a specific task. Instead of writing the same code multiple times, we can define a method once and call it whenever needed.

    Using methods helps in:

    • Making the code more organized and readable
    • Avoiding code repetition
    • Improving code reusability

    In this blog, we will cover:

    • What is a Method in Java?
    • How to Declare a Method
    • Calling a Method in Java
    • Example Programs

    1. What is a Method in Java?#

    A method is a group of statements that perform a specific operation. A method is defined once but can be called multiple times.

    Example of a Simple Method:#

    class Main { // Method to display a message static void greet() { System.out.println("Hello! Welcome to Java."); } public static void main(String[] args) { // Calling the method greet(); } }

    Expected Output:#

    Hello! Welcome to Java.

    2. How to Declare a Method in Java?#

    A method in Java is declared using the following syntax:

    returnType methodName(parameters) { // method body return value; // (only if returnType is not void) }

    Explanation of Method Components:#

    • returnType → The type of value the method returns. If it does not return anything, use void.
    • methodName → The name of the method (should be meaningful).
    • parameters → Input values passed to the method (optional).
    • method body → The set of statements that define what the method does.

    3. Calling a Method in Java#

    A method must be called to execute its code.

    Example: Calling a Method#

    class Main { // Method that prints a message static void displayMessage() { System.out.println("Java is fun!"); } public static void main(String[] args) { // Calling the method displayMessage(); } }

    Expected Output:#

    Java is fun!

    4. Example: Method with Parameters and Return Value#

    A method can take parameters (input values) and return a value using the return statement.

    Example: Adding Two Numbers Using a Method#

    class Main { // Method to add two numbers and return the sum static int addNumbers(int a, int b) { return a + b; } public static void main(String[] args) { // Calling the method and storing the result int sum = addNumbers(10, 20); // Printing the result System.out.println("Sum: " + sum); } }

    Expected Output:#

    Sum: 30

    5. Method Returning No Value (void Method)#

    If a method does not return any value, we use void.

    Example: Printing a Message Using a Void Method#

    class Main { // Method with no return value static void printWelcome() { System.out.println("Welcome to Java Programming!"); } public static void main(String[] args) { // Calling the method printWelcome(); } }

    Expected Output:#

    Welcome to Java Programming!

    6. Using Methods with Different Return Types#

    A method can return different data types, such as int, double, String, etc.

    Example: Returning a String from a Method#

    class Main { // Method returning a String static String getMessage() { return "Hello from the getMessage method!"; } public static void main(String[] args) { // Calling the method and printing the result System.out.println(getMessage()); } }

    Expected Output:#

    Hello from the getMessage method!

    7. Method with Multiple Parameters#

    A method can take multiple parameters separated by commas.

    Example: Multiplication of Two Numbers Using a Method#

    class Main { // Method to multiply two numbers static int multiply(int x, int y) { return x * y; } public static void main(String[] args) { // Calling the method with arguments int result = multiply(5, 4); // Printing the result System.out.println("Multiplication Result: " + result); } }

    Expected Output:#

    Multiplication Result: 20

    8. Calling a Method Multiple Times#

    A method can be called multiple times to perform the same task.

    Example: Reusing a Method#

    class Main { // Method to display a message static void sayHello() { System.out.println("Hello!"); } public static void main(String[] args) { // Calling the method multiple times sayHello(); sayHello(); sayHello(); } }

    Expected Output:#

    Hello! Hello! Hello!

    Conclusion#

    In this blog, we learned:

    • What methods are and why they are useful
    • How to declare and call methods in Java
    • How to pass parameters to methods
    • How to use return values in methods
    • How to reuse methods multiple times

    Using methods makes your Java programs cleaner, more efficient, and easier to maintain. Now try writing your own methods and experiment with different return types and parameters!

    Last updated on Apr 09, 2025