⏱️ 10 min read • Beginner Level • Lesson 33
A method in Java is a block of code that performs a specific task. Methods help you write reusable, organized, and easy-to-maintain programs.
Before learning methods, you should understand Java Program Structure, Java Variables, and Java Data Types. After this lesson, continue with Java Method Parameters.
A method is a group of statements written together to perform a particular operation. Instead of writing the same code again and again, you can define a method once and call it whenever required.
Methods are used everywhere in real applications. For example, an online shopping website may use separate methods for:
Instead of writing all logic inside the main() method,
developers divide the program into smaller reusable methods.
calculateTotal(),
applyDiscount(),
makePayment(),
and
sendEmail().
The general syntax of a Java method is:
accessModifier returnType methodName(parameters) {
// method body
}
| Part | Meaning |
|---|---|
| Access Modifier |
Defines where the method can be accessed from, such as
public, private, or protected.
If no access modifier is written, Java uses default access,
which allows access only within the same package.
|
| Return Type | Defines what type of value the method returns. Use void if it returns nothing. |
| Method Name | Name used to call the method. |
| Parameters | Input values passed to the method. |
| Method Body | Statements written inside method braces. |
If a method does not use public, private, or
protected, it has default access. This means it can be accessed
only by classes in the same package.
class DefaultAccessMethodExample {
void showMessage() {
System.out.println("This method has default access.");
}
public static void main(String[] args) {
DefaultAccessMethodExample obj = new DefaultAccessMethodExample();
obj.showMessage();
}
}
Output:
This method has default access.
showMessage() has no access modifier, so it gets
default access and can be used only inside the same package.
A method runs only when it is called. Static methods can be called directly from another static method in the same class, while instance methods need an object.
public class CallMethodExample {
static void greet() {
System.out.println("Hello Java");
}
public static void main(String[] args) {
greet();
}
}
Output:
Hello Java
Method Call ➜ Control Transfers to Method ➜ Statements Execute ➜ Return to Caller
Predefined methods are already available in Java libraries. You can use them without writing their body.
Examples include System.out.println(), Math.sqrt(), and String.length().
public class PredefinedMethodExample {
public static void main(String[] args) {
System.out.println("Java Methods");
System.out.println(Math.sqrt(25));
System.out.println("ProwessApps".length());
}
}
Output:
Java Methods 5.0 11
User-defined methods are methods created by the programmer to perform specific tasks.
public class UserDefinedMethodExample {
static void showMessage() {
System.out.println("This is a user-defined method.");
}
public static void main(String[] args) {
showMessage();
}
}
Output:
This is a user-defined method.
A void method performs a task but does not return any value.
public class VoidMethodExample {
static void printWelcome() {
System.out.println("Welcome to Java methods.");
}
public static void main(String[] args) {
printWelcome();
}
}
Output:
Welcome to Java methods.
A return type method performs a task and sends a value back to the caller using the return statement.
public class ReturnMethodExample {
static int square() {
return 5 * 5;
}
public static void main(String[] args) {
int result = square();
System.out.println("Square: " + result);
}
}
Output:
Square: 25
void, it must return a value of that type.
A parameterized method accepts one or more input values through parameters.
public class ParameterizedMethodExample {
static void showStudent(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
showStudent("Ayan", 20);
}
}
Output:
Name: Ayan Age: 20
A no-argument method does not accept any parameters.
public class NoArgumentMethodExample {
static void displayDateMessage() {
System.out.println("Today is a good day to learn Java.");
}
public static void main(String[] args) {
displayDateMessage();
}
}
Output:
Today is a good day to learn Java.
A static method belongs to the class. It can be called without creating an object.
public class StaticMethodExample {
static void showMessage() {
System.out.println("Static method called");
}
public static void main(String[] args) {
StaticMethodExample.showMessage();
}
}
Output:
Static method called
An instance method belongs to an object. You must create an object to call an instance method.
public class InstanceMethodExample {
void showMessage() {
System.out.println("Instance method called");
}
public static void main(String[] args) {
InstanceMethodExample obj = new InstanceMethodExample();
obj.showMessage();
}
}
Output:
Instance method called
A recursive method is a method that calls itself. Recursion is useful for problems that can be divided into smaller similar problems.
public class RecursiveMethodExample {
static int factorial(int number) {
if (number == 1) {
return 1;
}
return number * factorial(number - 1);
}
public static void main(String[] args) {
System.out.println("Factorial: " + factorial(5));
}
}
Output:
Factorial: 120
Getter and setter methods are commonly used to read and update private variables. Getter methods are also called accessor methods, and setter methods are also called mutator methods.
class Student {
private String name;
public void setName(String studentName) {
name = studentName;
}
public String getName() {
return name;
}
}
public class GetterSetterExample {
public static void main(String[] args) {
Student student = new Student();
student.setName("Ayan");
System.out.println(student.getName());
}
}
Output:
Ayan
This example uses static method, instance method, void method, return method, and parameterized method together.
public class JavaMethodsExample {
static void welcomeMessage() {
System.out.println("Welcome to Java Methods");
}
static int addNumbers(int a, int b) {
return a + b;
}
void displayResult(int result) {
System.out.println("Result: " + result);
}
public static void main(String[] args) {
welcomeMessage();
int sum = addNumbers(10, 20);
JavaMethodsExample obj = new JavaMethodsExample();
obj.displayResult(sum);
}
}
Output:
Welcome to Java Methods Result: 30
welcomeMessage() is a static void method.addNumbers() is a static parameterized method that returns an integer.displayResult() is an instance void method.| Method Type | Meaning |
|---|---|
| Predefined Method | Already available in Java libraries. |
| User-defined Method | Created by the programmer. |
| void Method | Does not return a value. |
| Return Type Method | Returns a value. |
| Parameterized Method | Receives input values. |
| No-Argument Method | Does not receive parameters. |
| Static Method | Belongs to the class. |
| Instance Method | Belongs to an object. |
| Recursive Method | Calls itself. |
| Getter/Setter Method | Used to read and update private variables. |
void method does not return a value.return.🧠 Test your understanding with a quick quiz
Topic: Methods | Language: Java
public class Test {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
}
}public class Test {
static void greet() {
System.out.println("Hello Java");
}
public static void main(String[] args) {
greet();
}
}public class Test {
static void show(String name) {
System.out.println(name);
}
public static void main(String[] args) {
show("Ayan");
}
}🎉 Great job! Continue learning Java step by step.