Instance Methods in Java
An instance method is a method that belongs to an instance of a class.
- We must create an object of that class (or have one already) to call an instance method.
- Instance methods can access and modify instance fields (object state) and can call other instance or static methods.
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: Method may return a value. Ex:- int, void, String, char, float, etc.
- method_name: This is the method name you can write anything as you write the variable name.
- method body: The method body describes what the method does with statements.
Example 2: Instance Method Without Parameter
public class Student{
String name;
int age;
// Instance method
void displayInfo(){
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args){
// Creating an object
Student s1 = new Student();
// Assigning values
s1.name = "Alice";
s1.age = 20;
// Calling instance method
s1.displayInfo();
}
}
Output
Name: Alice, Age: 20
Explanation:
Student classcontains instance variablesnameandage.- The
displayInfo()instance method prints their values. - We create an object
s1, assign values, and call the method to display them.
Example 2: 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)
- Used to read the value of private instance variables.
- Start with get followed by the variable name (first letter capitalized).
- Helps in encapsulation by providing controlled access to data.
2. Mutator Methods (Setters)
- Used to modify or update the value of private instance variables.
- Start with set followed by the variable name (first letter capitalized).
- Provides a controlled way to change data in a class.
Let's get understand by some examples:
import java.io.*;
class account {
// private variable-balance
private int balance = 50;
// accessor method (getter)
public int getBalance(){
return balance;
}
// Mutator method (setter)
public void setBalance(int a){
// return balance + a;
balance += a;
}
}
class GFG{
public static void main(String[] args){
account obj = new account();
// setting new value for balance
obj.setBalance(50);
// calling the Mutator (accessor)
System.out.println("Your Balance : "+ obj.getBalance());
System.out.println("GFG!");
}
}
Output
Your Balance : 100 GFG!
Explanation:
- The Account class defines a private variable balance and provides getBalance() and setBalance() methods.
- The setBalance() method increases the balance by a given amount.
- In main(), we update the balance and retrieve it using getter and setter methods.