When you start learning Java, one of the first and most important things to understand is a method. Methods are the building blocks of your program. They help you split the code into smaller, logical parts, where each part does one clear task.
What Is a Method
A method is a set of instructions that performs one action. For example, a method can print text, add two numbers, or check a password.
Think of a method as an instruction manual — it tells the computer what to do, but it doesn’t run automatically.
When we write a method, it’s only a description. It doesn’t start by itself. To make it work, we have to call it — like turning on a lamp with a switch. The lamp exists (the method is written), but it won’t shine until you switch it on (call the method).
public class Example {
public static void sayHello() {
System.out.println("Hello world!");
}
}
Explanation:
public
— the method can be used from other classes (other Java files).static
— means it can be called without creating an object.void
— means it doesn’t return a value (What Does return Do in Programming?).sayHello()
— the method’s name.{ ... }
— the block of code that runs when the method is called.
Calling a Method in the Same Class
A method can be called from another method in the same class. Usually, we call methods from inside the main method.
public class Greeting {
public static void main(String[] args) {
sayHello(); // call a method in the same class
sayGoodbye(); // call another method
}
public static void sayHello() {
System.out.println("Hi!");
}
public static void sayGoodbye() {
System.out.println("Bye!");
}
}
main()
is the entry point of the program — it runs first.- When Java sees
sayHello();
, it jumps to that method, runs the code inside it, and then goes back tomain()
. - After that, it runs
sayGoodbye();
. - Both methods are in the same class and don’t take any input or return anything.
- Because they are
static
, we can call them just by their names.
Why We Can Call a Method Written Below
In the example above, the methods sayHello()
and sayGoodbye()
are written after main()
, but the program still works. This happens because Java is a compiled language. Before running your program, the compiler reads the whole class and knows all the methods, no matter where they are in the file. The order of writing doesn’t affect how the program runs — it only affects how easy the code is to read.
Calling a Method from Another Class (static)
If the method is in another class, we can call it using the class name and a dot.
public class Example {
public static void sayHello() {
System.out.println("Hello world!");
}
}
public class Main {
public static void main(String[] args) {
Example.sayHello(); // call a method from another class
}
}
Here sayHello()
is static
, so we can call it directly with Example.sayHello()
— no need to create an object.
Calling a Method from Another Class (using an object)
If a method is not static
, it belongs to an object, not the class itself. So we must create an object first.
// MathHelper.java
public class MathHelper {
public int addNumbers(int a, int b) {
return a + b;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
MathHelper helper = new MathHelper(); // create an object
int result = helper.addNumbers(5, 3); // call the method
System.out.println("Result: " + result);
}
}
Output:
Result: 8
Static vs Normal (Instance) Methods
Example | Type of method | Need to create an object? |
---|---|---|
Example.sayHello(); | static | No |
helper.addNumbers(5,3); | instance | Yes |
Key Points to Remember
- A method is a small piece of code that performs one task.
- You can call methods inside the same class or from another class.
- The Java compiler already knows all methods in your class, so their order doesn’t matter.
static
methods belong to the class.- Normal (instance) methods belong to objects.
- Later you will learn when to use each type depending on your program’s needs.