-
Hajipur, Bihar, 844101
In Java, methods are blocks of code that perform specific actions. They define the behavior of objects and help make your code reusable, organized, and easy to maintain. While attributes store the state of an object, methods define what the object can do.
For example, a Car object may have attributes like color and speed, but methods like start(), accelerate(), and stop() describe its behavior. Understanding methods is essential to mastering Object-Oriented Programming because they allow classes to interact with each other and perform tasks dynamically.
A method in Java is a group of statements that perform a specific operation. It is defined inside a class and can be called using objects.
The basic syntax of a method looks like this:
returnType methodName(parameters) {
// method body
}
Where:
returnType – The type of value the method returns (int, String, void, etc.)
methodName – The name of the method (should describe what it does)
parameters – Optional input values passed to the method
method body – The actual block of code that runs when the method is called
Example:
void greet() {
System.out.println("Hello, welcome to Java!");
}
Here, greet() is a simple method that prints a message.
Let’s look at how to define and call methods in Java.
class Student {
void displayInfo() {
System.out.println("Student details are displayed here.");
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Create an object
s1.displayInfo(); // Call method using object
}
}
Output:
Student details are displayed here.
Here:
displayInfo() is a method defined inside the Student class.
The method is called using the object s1 with the dot operator.
A method can also take parameters — values passed from outside — to perform operations dynamically.
Example:
class Calculator {
void add(int a, int b) {
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
c.add(10, 20); // Passing arguments
}
}
Output:
Sum: 30
Here, add() accepts two parameters (a and b) and prints their sum.
Some methods don’t just perform actions; they return values that can be used later. To do this, replace the return type from void to the appropriate data type and use the return keyword.
Example:
class Calculator {
int multiply(int x, int y) {
return x * y;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.multiply(4, 5);
System.out.println("Result: " + result);
}
}
Output:
Result: 20
Here, the method returns a value that is stored in result.
The keyword void means the method doesn’t return any value. You use it when the method performs an action but doesn’t need to send back data.
Example:
void showMessage() {
System.out.println("This method does not return anything.");
}
When you call this method, it executes the statement but doesn’t return a value.
You can also call one method inside another. This helps in structuring code neatly.
Example:
class Message {
void displayGreeting() {
System.out.println("Good Morning!");
}
void displayFullMessage() {
displayGreeting(); // Calling another method
System.out.println("Have a great day!");
}
public static void main(String[] args) {
Message msg = new Message();
msg.displayFullMessage();
}
}
Output:
Good Morning!
Have a great day!
A static method belongs to the class rather than any specific object. It can be called directly using the class name without creating an object.
Example:
class MathUtils {
static void square(int num) {
System.out.println("Square: " + (num * num));
}
public static void main(String[] args) {
MathUtils.square(5); // Calling static method using class name
}
}
Output:
Square: 25
Static methods are often used for utility functions or operations that don’t depend on object data.
An instance method depends on object data and can only be called using an object. It can access both instance variables and static variables.
Example:
class Student {
String name;
void introduce() {
System.out.println("My name is " + name);
}
}
You need to create an object to call this method:
Student s = new Student();
s.name = "Riya";
s.introduce();
Output:
My name is Riya
You can also pass entire objects to methods. This allows you to access and modify their data inside another method.
Example:
class Student {
String name;
int marks;
}
class School {
void display(Student s) {
System.out.println(s.name + " scored " + s.marks + " marks.");
}
public static void main(String[] args) {
Student st = new Student();
st.name = "Anjali";
st.marks = 88;
School sch = new School();
sch.display(st); // Passing object as argument
}
}
Output:
Anjali scored 88 marks.
Java allows you to define multiple methods with the same name but different parameter lists. This is called method overloading.
Example:
class Display {
void show(String name) {
System.out.println("Name: " + name);
}
void show(int age) {
System.out.println("Age: " + age);
}
}
Depending on what you pass, Java decides which version of show() to call.
Methods define what an object can do.
You can call methods using objects with the dot operator.
Use parameters to pass data to methods.
Methods can return values using the return statement.
void means the method does not return anything.
Use static methods when you don’t need to create an object.
Instance methods depend on object data.
Methods can be called from within other methods.
In this tutorial, you learned how methods work in Java — how to define, call, and return values from them. Methods make Java programs modular and reusable by grouping actions into logical units.
Understanding methods prepares you for more advanced OOP topics like method parameters, method overloading, and constructors, which we’ll cover in the upcoming chapters.
1. Write a Java program with a method displayMessage() that prints “Welcome to Java Methods!” when called from the main() method.
2. Create a class MathOperation with a method addNumbers(int a, int b) that prints the sum of two numbers. Call this method from main() using an object.
3. Write a method getSquare(int number) that returns the square of a given number. Display the result in the main() method.
4. Create a class Student with a method showDetails(String name, int age) that prints student information. Call this method from main() by passing arguments.
5. Write a program that has a method findMaximum(int a, int b) which returns the greater of two numbers. Display the result in the main() method.
6. Create a static method printTable(int number) that prints the multiplication table of a number. Call it directly from main() without creating an object.
7. Write a program with a method calculateAverage(int n1, int n2, int n3) that returns the average of three numbers. Print the result in main().
8. Create a class Circle with a method area(double radius) that returns the area of the circle. Display the area in main().
9. Write a program where one method calls another. Example: displayWelcome() calls printName(String name) to print a welcome message.
10. Create a class Employee with attributes name and salary, and a method showInfo() that displays both. Write another method increaseSalary(double amount) that adds the given amount to the salary. Demonstrate both methods in main().