An instance method belongs to an object of a class and requires an instance to be called. It can access and modify the object’s instance variables and can also call other instance or static methods.
- Can access and modify instance variables (object state).
- Can accept parameters and return a value.
- Can call other instance methods or static methods.
- Supports encapsulation and object-oriented design.
- Follows the divide-and-conquer principle in larger applications.
class Person {
String name = "Alice";
// Instance method
void greet()
{
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args)
{
Person p = new Person();
p.greet(); // Calling instance method
}
}
Output
Hello, Alice!
Explanation:
- greet() is an instance method.
- It prints the name of the object.
- Called using an object p of the Person class.
Syntax
modifier return_type method_name( )
{
method body ;
}
- modifier: It defines the access type of the method, and it is optional to use.
- return_type:Type of value returned by the method (int, void, String, etc.).
- method_name: Name of the method.
- method body: The method body describes what the method does with statements.
Example: Instance Method With Parameter
import java.io.*;
class GFG{
// static method
public static void main (String[] args){
// creating object
GFG obj = new GFG();
// calling instance method by passing value
obj.add(2,3);
System.out.println("GFG!");
}
// Instance method with parameter
void add(int a, int b){
// local variables
int x= a;
int y= b;
int z= x + y;
System.out.println("Sum : " + z);
}
}
Output
Sum : 5 GFG!
Explanation:
GFGclass has an instance methodadd()that accepts two integers.- The method adds the numbers and prints their sum.
- We create an object and call
add()with parameters2and3.
Types of Instance Methods:
There are two types of Instance methods in Java:
1. Accessor Methods (Getters)
Getter methods are used to retrieve the value of a private instance variable. They provide controlled access to object data without allowing modification.
class Account {
private int balance = 100;
// Accessor method (getter)
public int getBalance() { return balance; }
public static void main(String[] args)
{
Account acc = new Account();
System.out.println(
"Balance: "
+ acc.getBalance()); // Calling getter
}
}
Output
Balance: 100
Explanation:
- Returns the value of a private instance variable.
- Provides controlled access to the data.
2. Mutator Methods (Setters)
Setter methods are used to modify or update the value of a private instance variable. They allow controlled updates while maintaining encapsulation.
class Account {
private int balance = 100;
// Mutator method (setter)
public void setBalance(int amount)
{
balance += amount;
}
public int getBalance() { return balance; }
public static void main(String[] args)
{
Account acc = new Account();
acc.setBalance(50); // Updating balance
System.out.println(
"Balance: "
+ acc.getBalance()); // Calling getter
}
}
Output
Balance: 150
Explanation:
- Modifies the value of a private instance variable.
- Works together with a getter to control access to data.