DEV Community

Cover image for Methods in Java Explained with Real Examples
naveen kumar
naveen kumar

Posted on

Methods in Java Explained with Real Examples

When you start learning Java, one concept quickly becomes essential:

๐Ÿ‘‰ Methods

At first, writing code line by line works. But as programs grow:

โœ“ Code becomes repetitive
โœ“ Logic becomes hard to manage
โœ“ Debugging becomes difficult

๐Ÿ‘‰ This is where methods in Java come in.

They help you write clean, reusable, and efficient code โ€” just like real-world applications.

๐ŸŽฏ What is a Method in Java?

A method in Java is a block of code that performs a specific task.

๐Ÿ‘‰ It runs only when called.

๐Ÿ‘‰ In simple terms:
Method = Reusable code block

๐Ÿงฉ Basic Syntax

returnType methodName(parameters) {
    // method body
}
Enter fullscreen mode Exit fullscreen mode

โœ… Example

public void greet() {
    System.out.println("Hello, Welcome!");
}
Enter fullscreen mode Exit fullscreen mode

โšก Why Methods are Important

Methods are the backbone of Java programming.

They help you:

โœ“ Avoid code repetition
โœ“ Improve readability
โœ“ Break large programs into smaller parts
โœ“ Simplify debugging
โœ“ Build modular applications

๐Ÿ‘‰ Without methods, large programs become messy.

๐Ÿง  Types of Methods in Java

๐Ÿ”น 1. Predefined Methods

Built-in methods provided by Java.

System.out.println("Hello World");
Enter fullscreen mode Exit fullscreen mode

โœ“ Already available
โœ“ Easy to use

๐Ÿ”น 2. User-Defined Methods

Created by developers.

public void display() {
    System.out.println("User-defined method");
}

Enter fullscreen mode Exit fullscreen mode

โœ“ Custom logic
โœ“ Reusable

๐Ÿ”น 3. Static Methods

Belong to class (no object needed).

public static void show() {
    System.out.println("Static Method");
}
Enter fullscreen mode Exit fullscreen mode

โœ“ Called using class name
โœ“ No object required

๐Ÿ”น 4. Instance Methods

Require object to call.

public void printMessage() {
    System.out.println("Instance Method");
}

Enter fullscreen mode Exit fullscreen mode

โœ“ Object-based
โœ“ Used for real-world behavior

๐Ÿ”„ Methods with Parameters

Methods can take input values.

public void add(int a, int b) {
    System.out.println(a + b);
}

Enter fullscreen mode Exit fullscreen mode

Calling:

add(5, 3);

๐Ÿ‘‰ Makes methods flexible.

๐Ÿ” Methods with Return Type

Methods can return results.

public int multiply(int a, int b) {
    return a * b;
}
Enter fullscreen mode Exit fullscreen mode

โœ“ Returns value
โœ“ Used in calculations

๐Ÿ”€ Method Overloading

Same method name, different parameters.

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

public int add(int a, int b, int c) {
    return a + b + c;
}
Enter fullscreen mode Exit fullscreen mode

โœ“ Improves flexibility
โœ“ Cleaner code

๐Ÿ”„ Method Overriding

Child class changes parent method.

class Animal {
    void sound() {
        System.out.println("Animal sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

Enter fullscreen mode Exit fullscreen mode

โœ“ Supports polymorphism
โœ“ Used in OOP

๐Ÿš€ Main Method in Java

Every program starts here.

public static void main(String[] args) {
    System.out.println("Program starts here");
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Entry point of execution.

๐ŸŒ Real-World Examples

๐Ÿงฎ Calculator

public int add(int a, int b) {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Login System

public boolean login(String username, String password) {
    return username.equals("admin") && password.equals("1234");
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽ“ Student Grade

public char grade(int marks) {
    if(marks >= 90) return 'A';
    else return 'B';
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Methods are used everywhere.

๐Ÿงฑ Key Components of a Method

Every method includes:

โœ“ Access modifier
โœ“ Return type
โœ“ Method name
โœ“ Parameters
โœ“ Method body

โœ… Best Practices

โœ“ Use meaningful method names
โœ“ Keep methods short
โœ“ Avoid complexity
โœ“ Write reusable code
โœ“ Follow proper formatting

Common Mistakes

Forgetting return
โœ“ Always return correctly

Confusing overloading & overriding
โœ“ Understand difference

Not calling methods
โœ“ Always invoke method

๐ŸŽฏ Why Methods Matter for Your Career

If you want to become:

โœ“ Java Developer
โœ“ Backend Engineer
โœ“ Full Stack Developer

Methods help you:

โœ“ Build real applications
โœ“ Write clean code
โœ“ Improve performance

FAQs

What is a method?

โœ“ Reusable code block

Types of methods?

โœ“ Predefined, user-defined, static, instance

What is overloading?

โœ“ Same name, different parameters

What is overriding?

โœ“ Redefining parent method

Static method?

โœ“ No object needed

Instance method?

โœ“ Needs object

Return type?

โœ“ Value returned

Main method?

โœ“ Program entry point

๐Ÿ Final Thoughts

Methods in Java are the foundation of programming.

They help you:

โœ“ Write clean code
โœ“ Reuse logic
โœ“ Build scalable systems

๐Ÿ‘‰ Start practicing, write your own methods, and build small projects.

Thatโ€™s how you become a strong Java developer ๐Ÿš€

Top comments (0)