Definition of method:-
These are also known as functions, procedures, modules, subroutines,sub programs. In simple words it is a collection of statements which are going to perform a particular task. They don’t do anything until they are called(executed). They can be used(called)as many number of times as required.
The syntax of the method is given below.
[Access specifier][type][return type][name of the method](Argument list)
// Method code
public static void main(String args)
There are two types of methods.
- System defined methods: They are also called library methods, built in methods.these are defined by java. All the methods of Math class, String class are example of system defined methods.
- User defined methods:- The methods defined by user are called user defined methods.
Advantages of methods:-.
-Code re usability
-Divide complex Programs into manageable code. -Improves the readability.
-Makes debugging easier.
Method (function)definition:- It has two parts method header and method body.
Method (function)prototype: The first line of the method definition is called method prototype/method header.
Example public add(int a , int b)
Method(function) signature: Method name and list of parameters is called method signature.
Example add(int a, int b)
Types of methods:
Pure Method:- A method that does not change the original state of an object is called a pure method. It always return the same value when the same arguments are given.
Impure Method:- A method that changes the original state of an object is called an Impure method. It does not return the same value when the same arguments are given.
The method which can be accessed without an instance of the class are known as static method. It can be invoked using the class name(Math.pow(2.0,3.0)) or only with the method name also(add(4,5)).
These methods are created with static keyword.
Non static method:
The method which are accessed through an instance of the class are known as non static method.
Student obj=new Student();
These methods are not created with static keyword.