Instance Methods are the group of codes that performs a particular task. Sometimes the program grows in size, and we want to separate the logic of the main method from other methods. A method is a function written inside the class. Since java is an object-oriented programming language, we need to write a method inside some classes.
The important points regarding instance variables are:
- Instance methods can access instance variables and instance methods directly and undeviatingly.
- Instance methods can access static variables and static methods directly.
Tóm Tắt
Instance Method without parameter
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:
public void disp( ) { int a= 10; System.out.println(a); }
Calling Instance Method:
You can not call an instance method in the static method directly, so the Instance method can be invoked using an object of the class. We know that the java program’s execution starts from the main method and the main method is static, so we can not directly call the instance method. We have to create the class object; then, we can call the instance method in the main method.
Let’s see how we can call the Instance method:
Example 1:
Java
import
java.io.*;
class
GFG {
public
static
void
main (String[] args) {
GFG obj =
new
GFG();
obj.disp();
System.out.println(
"GFG!"
);
}
void
disp()
{
int
a =
20
;
System.out.println(a);
}
}
Output
20 GFG!
Example 2:
Java
import
java.io.*;
class
class1 {
void
add()
{
int
a=
2
;
int
b=
3
;
System.out.println(
"The sum of 2 and 3 is :"
+ (a+b));
}
}
class
GFG {
public
static
void
main (String[] args) {
class1 obj =
new
class1();
obj.add();
System.out.println(
"GFG!"
);
}
}
Output
The sum of 2 and 3 is :5 GFG!
Instance Method With Parameter
Instance method with parameter takes the argument when it is called in the main method. Now let’s see Examples for better understanding.
Syntax:
modifier return_type method_name( parameter list) { method body ; }
- Parameter List: The list of parameters separated by a comma. These are optional; the method may contain zero parameters.
Example:
public void disp(int a, int b) { int x=a ; int y=b; int z = x+y; System.out.println(z); }
Java
import
java.io.*;
class
GFG {
public
static
void
main (String[] args) {
GFG obj =
new
GFG();
obj.add(
2
,
3
);
System.out.println(
"GFG!"
);
}
void
add(
int
a,
int
b)
{
int
x= a;
int
y= b;
int
z= x + y;
System.out.println(
"Sum : "
+ z);
}
}
Output
Sum : 5 GFG!
Types of Instance Methods:
There are two types of Instance methods in Java:
- Accessor Method (Getters)
- Mutator Method (Setters)
The accessor method is used to make the code more secure and increase its protection level, accessor is also known as a getter. Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc. For the convenience of the program, getter starts with the word “get” followed by the variable name.
The mutator method is also known as the setter. It sets the value for any variable which is used in the programs of a class. and starts with the word “set” followed by the variable name. Getter and Setter make the programmer convenient in setting and getting the value for a particular data type. In both getter and setter, the first letter of the variable should be capital.
Accessor and mutator are mainly used to access or set the value for the private member of the class in the main method.
Let’s get understand by some examples:
Java
import
java.io.*;
class
account {
private
int
balance =
50
;
public
int
getBalance()
{
return
balance;
}
public
void
setBalance(
int
a)
{
balance += a;
}
}
class
GFG {
public
static
void
main(String[] args)
{
account obj =
new
account();
obj.setBalance(
50
);
System.out.println(
"Your Balance : "
+ obj.getBalance());
System.out.println(
"GFG!"
);
}
}
Output
Your Balance : 100 GFG!
My Personal Notes
arrow_drop_up