METHODS
A method represents a group of statements to perform a task, methods are written always inside a class.
A method has two parts.
1. Method header/proto type
2. Method body
Method header/proto type:
Syntax:
Return type method Name (parameters)
{
}
Method header/proto type contains method names, parameters / arguments to receive data from outside and the return data type
Ex :
Double sum(double x,double y)
{
}
Note: If a method not returning any value then given the return type as void.
Method body:
Method body contains a block of statement representing the logical to perform a task.
Ex :-
Double sum(double x,double y)
{
Double z=x+y;
Return z;
}
TYPES OF METHODS
![Java Methods: A Comprehensive Guide 1 java methods](https://anuragedutech.com/wp-content/uploads/2024/02/java-methods-1024x683.png)
Types of Methods:
There are three types of methods
1. Static method.
2. Instance method.
3. Factory method.
Static Method
- It is a method that does not act up on instance variable of a class directly.
- Static methods are declared using keyword static.
- Static methods do not required class object to call them.
- Static methods cannot access instance variables, but static methods can acessstatic variables.
Instance Method
- Instance methods are the methods which interact with instance variables of a
class. - These methods need an object to be a class.
- There are two types of instance methods.
1. Accessor methods.
2. Mutator methods.
Access or Method
- Accessor methods are those methods which accessor read the instance
variables. - They will not modify the instance variables of a class.
- The content of object will not modify by these methods
Mutator Method
- Mutator Method modifies the content object.
Factory Method
- A factory method is a method that return object of that class which it is a member.
- Factory methods are static methods.