Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 15

Methods in Java

In this lesson: Create and call methods in Java — parameters, return types, static methods and overloading — with output. In Hindi & English.

What is a method

A method is a named block of code that performs a task. You write it once and call it whenever you need it, avoiding repeated code. Even main is a method. Methods break a big program into small, reusable, easy-to-read pieces.

💡 Define a method once, call it many times — that is how you avoid repeating code.

Defining a method

public class Main {
    static void greet() {
        System.out.println("Hello!");
    }
    public static void main(String[] args) {
        greet();      // call the method
        greet();      // call again
    }
}
Hello!
Hello!

We define greet() once, then call it twice from main. The code inside runs each time it is called.

Parameters

static void greet(String name) {
    System.out.println("Hello, " + name);
}
// call:
greet("Aman");
greet("Diya");
Hello, Aman
Hello, Diya

Parameters let you pass information in. name is a parameter; "Aman" is the argument you pass. The method uses whatever value it receives.

Returning a value

static int add(int a, int b) {
    return a + b;
}
// call:
int sum = add(5, 3);
System.out.println(sum);
8

A method can send a value back with return. Its return type (int) must match. Here add returns the sum, which we store in sum.

void vs return

  • void — the method does something but returns nothing (like printing). No return value.
  • return type — the method gives a value back (like int, String) using return.

Choose void when the method just performs an action, and a return type when you need a result from it.

Static methods

static int square(int n) {
    return n * n;
}
// called without an object:
System.out.println(square(4));   // 16
16

A static method belongs to the class, so you can call it directly (like square(4)) without creating an object. main is static, so it can call other static methods directly.

Common mistakes

  • Return type mismatch. A method declared int must return an int.
  • Forgetting return. A non-void method must return a value on every path.
  • Wrong argument count or type. Arguments must match the parameters.
  • Calling non-static from static without an object. A static main cannot call a non-static method directly.

Summary

  • A method is a reusable named block of code for one task.
  • Parameters take input; arguments are the values you pass.
  • Use return to send a value back; the return type must match.
  • void returns nothing; a return type gives a value.
  • static methods are called on the class without an object.

Next, learn reuse with the same name in overloading vs overriding, or the static keyword.

Frequently Asked Questions

What is a method in Java?
A method is a named block of code that does a specific task. You define it once and call it whenever needed, which avoids repeating code and keeps programs organised. main is itself a method.
What are parameters and arguments?
Parameters are the inputs a method declares (like int a, int b). Arguments are the actual values you pass when calling it (like add(5, 3)). Parameters receive the arguments.
What is the difference between void and a return type?
A void method does a task but gives nothing back. A method with a return type (like int) sends a value back with return, which you can store or use.
What does static mean for a method?
A static method belongs to the class, not to an object, so you can call it without creating an object. main is static. Non-static methods need an object to be called on.
Why use methods instead of writing everything in main?
Methods let you reuse code, avoid repetition, and break a big problem into small, named steps. This makes programs easier to read, test and fix, especially as they grow.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicOOP Concepts Your progress: 15 / 61 (25%) · Lesson 16 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free