HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›Methods

Methods

static methods, parameters and return types, void.

Simple enough for a 5-year-old to understand

static method and return

methods to be called from main must be static. Return type, name, and parameters are written.

Example code
1
public class Main {
2
static int square(int x) {
Takes an int, returns an int.
FunctionTakes an int, returns an int.
3
return x * x;
Result.
FunctionResult.
4
}
5
public static void main(String[] args) {
6
System.out.println(square(5));
25
Output / Input25
7
}
8
}
FunctionOutput / Input
💡 Hint: void return type means 'returns nothing' (just does the work).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write a static method named addUp that takes two ints and returns their sum; print addUp(8, 9).

🎯 Expected output: 17

Loading editor…

About this topic

A method is giving a name to a piece of code that does a job — what is called a function in other languages is Java's version. In Java, every method lives inside a class; you cannot write free-floating methods outside of a class.

A method signature consists of three things: the return type, the name, and the parameter list. The return type tells what the method will give back; if it returns nothing, void is written. Because Java is strongly typed, this information is mandatory, and the compiler checks every call accordingly.

Java allows writing multiple methods with the same name, as long as their parameters are different. This is called overloading and is the standard way to do the same task with different types.

What you will learn

  • Defining and calling methods
  • Using return types and void
  • Passing parameters
  • The difference between static methods and instance methods
  • Method overloading
  • How value and reference are passed

When to use?

If you are about to write a block of code for the second time, it is time to extract it into a method. Also, when a method starts to become longer than half your screen, breaking it down into smaller methods makes maintenance easier.

Helper operations that do not need the object's state are static; operations that work with the object's fields are instance methods.

Common mistakes

Wrong

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

The return type is declared as int, but the method returns nothing; the compiler gives "error: missing return statement". Every method specifying a return type must return a value.

Correct

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

Wrong

public class Main {
    public void greet() {
        System.out.println("Hello");
    }
    public static void main(String[] args) {
        greet();
    }
}

The main method is static, while greet is an instance method. You cannot directly call an instance method from a static context; you will get "error: non-static method greet() cannot be referenced from a static context".

Correct

public class Main {
    public void greet() {
        System.out.println("Hello");
    }
    public static void main(String[] args) {
        new Main().greet();
    }
}

Wrong

public static void reset(int value) {
    value = 0;
}
// call:
int count = 5;
reset(count);

Java passes parameters by VALUE; assigning a new value to a parameter inside the method does not affect the variable on the calling side. count still remains 5. The way to change the value is to return it.

Correct

public static int reset(int value) {
    return 0;
}
// call:
int count = 5;
count = reset(count);

Frequently asked questions

What is the difference between a static method and an instance method?

A static method belongs to the class and is called without creating an object; it cannot access the object's fields. An instance method belongs to an object and works with that object's fields. Helper calculations are static, while operations using the object's state are instance methods.

Does Java pass by value or by reference?

It always passes by value — but for objects, the value passed is a copy of the REFERENCE. Therefore, if you modify the contents of the object inside the method, this is visible outside; however, if you assign a new object to the parameter, the external variable is not affected. This distinction is frequently confused.

What is method overloading?

It is writing multiple methods with the same name and different parameter lists. The compiler chooses which version is called by looking at the types of the arguments. Providing the same job separately for int and double is the classic example; merely changing the return type does not count as overloading and results in an error.

Summary

In Java, every method lives inside a class, and its signature consists of the return type, name, and parameters. Calling an instance method from a static context is an error, and since parameters are passed by value, assignment inside the method does not affect the caller.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code