What Are Methods in Java?
A method in Java is a small, well-defined block of code that performs a specific task in your program. We don’t need to repeat the same lines of code in multiple places; just write that logic once inside a method and call it whenever needed.
Methods make our program more organized, readable, and reusable. They are helping us to divide a large program into smaller, manageable sections, each focusing on a single function or operation.
For example, if you need to calculate the sum of numbers multiple times in a program, you can simply create a method called calculateSum() and use it whenever required.
Why Are Methods Important In Java?
Let’s understand the features of Java methods in simple terms:
- Reusability:
- One of the biggest advantages of using methods is code reusability. Instead of writing the same set of instructions again and again, you can define them once inside a method and call that method whenever needed.
- Modularity:
- Methods break a large program into smaller, logical parts called modules. Each method performs a specific task, which makes it easier to focus on one part of the code at a time.
- Improved Readability:
- Methods make your code clean and readable. When your logic is divided into clearly named methods like calculateSum() or displayResult(), anyone reading your code can quickly understand what each part does without needing to go through all the internal details.
- Parameters:
- Methods can take parameters (or arguments) that allow them to perform tasks dynamically. Instead of working with fixed values, you can pass data into methods when calling them.
- Return Values:
- A method can return a result after performing its task. The returned value can then be stored or used in other parts of the program.
Syntax of a Method in Java
This syntax defines how a method is written, how it works, and what it can do. Let’s first look at the general syntax and then understand each part step by step.
accessModifier returnType methodName(parameters) {
// Method body (logic or operations)
}
Explanation of Each Part:
1) Access Modifier: The access modifier defines the visibility or accessibility of a method — in other words, who can use it. It indicates to the Java program whether the method can be accessed from other classes, within the same package, or only within the same class.
For example:
- public → The method can be accessed from anywhere in the program.
- private → This method can be accessed only within the same class.
- protected → This can be accessed within the same package or subclasses.
- (default) → No modifier means the method is accessible only within the same package.
2) Return Type: The return type specifies what kind of value the method will return after executing. If a method performs an operation but doesn’t return any value, its return type is written as void.
- int → returns an integer value
- String → returns a string value
- double → returns a decimal value
- void → returns nothing
I’d like you to please learn in detail about Data Types in Java.
3) Method Name: The method name is used to identify and call the method. It should be meaningful and written using camelCase naming style (the first letter lowercase, next words capitalized).
For example:
void printSum() {
System.out.println("Sum printed");
}
4) Parameters: Parameters act as inputs for the method. They allow you to pass data or values when calling the method, making it flexible and reusable.
For example:
void greetUser(String name) {
System.out.println("Hello, " + name + "!");
}
// Here, String name is a parameter that receives a value when the method is called.
5) Method Body: The method body is the block of code enclosed within { } where all the logic is written. It defines what the method actually does, whether it performs calculations, displays messages, or processes data.
For example:
Example:
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
Types of Methods in Java
There are mainly two types of methods in Java: Predefined Methods and User-Defined Methods.
1) Predefined Methods:
These are built-in methods that come with Java’s standard libraries. They are already written and tested by the Java developers, so you can directly use them in your programs without writing their logic yourself.
These methods save time and make coding easier, as you don’t have to create everything from starting.
Examples of predefined methods:
- System.out.println() → used to print messages on the screen.
- Math.sqrt( ) → used to find the square root of a number.
- Math.max( ) → returns the largest of two numbers.
Example:
public class PredefinedExample {
public static void main(String[] args) {
int number = 16;
// Using predefined method to find square root
double result = Math.sqrt(number);
System.out.println("Square root of " + number + " is: " + result);
}
}
Output:
Square root of 16 is: 4.0
In this example, Math.sqrt() is a predefined method that returns the square root of the given number.
2) User-Defined Methods:
These are methods created by the programmer to perform specific tasks. When you need a method that doesn’t exist in Java libraries, you can define your own.
This helps reduce code repetition and makes your program modular and organized.
For example:
public class MyClass {
// User-defined method to add two numbers
public int addNumbers(int a, int b) {
return a + b;
}
public static void main(String[] args) {
MyClass obj = new MyClass(); // Create object to access method
int sum = obj.addNumbers(10, 15); // Call user-defined method
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 25
- Here, the addNumbers() is a user-defined method created by the programmer.
Void vs. Return Methods
In Java, methods can be classified further based on whether they return a value or just perform an action.
1) Void Method
A void method performs some action but does not return any value. You just call it, and it executes its task. For example:
public class DisplayMessage {
public void showMessage() {
System.out.println("Welcome to Java Programming!");
}
public static void main(String[] args) {
DisplayMessage obj = new DisplayMessage();
obj.showMessage(); // Calling the void method
}
}
Output:
Welcome to Java Programming!
- In this code, the method showMessage() only displays a message; it doesn’t return anything, so its return type is void.
2) Method with Return Type
A return type method performs a task and returns a result back to the calling code. This type of method is used when you need to use the output value later in your program.
For example:
public class SquareCalculator {
public int square(int number) {
return number * number; // Returning a value
}
public static void main(String[] args) {
SquareCalculator obj = new SquareCalculator();
int result = obj.square(6); // Capture the returned value
System.out.println("Square: " + result);
}
}
Output:
Square: 36
- Here, the method square() takes a number, multiplies it by itself, and returns the result.
What Is Method Parameters In Java?
What Is Method Overloading In Java?
What is Static Methods in Java?
In Java, a static method is a method that belongs to the class itself, not to any specific object. This means you don’t need to create an object of the class to call it; you can directly access it using the class name.
Static methods are often used for utility tasks or operations that don’t depend on instance variables, such as mathematical calculations or displaying general information.
When a method is declared with the static keyword, it becomes a class-level method.
Syntax of a Static Method:
class ClassName {
static returnType methodName(parameters) {
// Method body
}
}
In this code:
- static is a keyword that makes the method belong to the class.
- returnType is the data type of the value the method returns (use void if it returns nothing).
- methodName is the name of the method.
- parameters is the optional values passed into the method.
Real-World Example of Static Method in Java
public class MyClass {
// Defining a static method
public static void displayInfo() {
System.out.println("Static methods belong to the class, not to objects.");
}
public static void main(String[] args) {
// Calling static method without creating an object
MyClass.displayInfo();
}
}
Output:
Static methods belong to the class, not to objects.
- The method displayInfo() is declared with the static keyword.
- It is called directly using the class name (MyClass.displayInfo()), without creating any object.
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.
- What is Java if else statements?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.