![]() |
VOOZH | about |
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.
Hello, Alice!
Explanation:
modifier return_type method_name( )
{
method body ;
}
Example: Instance Method With Parameter
Sum : 5 GFG!
Explanation:
GFG class has an instance method add() that accepts two integers.add() with parameters 2 and 3.There are two types of Instance methods in Java:
Getter methods are used to retrieve the value of a private instance variable. They provide controlled access to object data without allowing modification.
Balance: 100
Explanation:
Setter methods are used to modify or update the value of a private instance variable. They allow controlled updates while maintaining encapsulation.
Balance: 150
Explanation: