Method Notes
Method Notes
-------
Method is nothing but a block of statements which is intended to achieve certain task.
It provides the reusability of code.
We can also easily modify code using methods. In turn, maintenance becomes easy.
A method can be called from 'n' programs/another methods, if a change is required in a
method, we have to make changes at one place (inside a method).
We write a method once and use it many times.
A method should be called in order to execute it.
We do not require to write code again and again. It also provides the easy modification and
readability of code, just by adding or removing a chunk of code. The method is executed only
when we call or invoke.
Method declaration:
------------------
Method declaration has 6 components
return type or void: we should write either void when method does not return any value to
its caller.
If method returns some value, then the data type of return value is mentioned in the
method declaration.
Method Name: Method name should start with lower case letter and follow camel case
standard. Usually it starts with a verb (eg: getPrice, getName, getValue, setValue). It can
have any no of characters/letters. eg:getPrice, setCustomerName.
Method name should reflect its functionality.
Parameter list: It defines a list of parameters along with its datatype and name. Sequence of
parameters are very important. Sequence of arguments while calling a method and the
parameter list sequence should be same. Complier will understand data types only (and not
variable names).
A method can have ZERO parameter or can have 1/n parameters depending on the
requirement of the method functionality.
Method body: It contains a block of statements which will achieve the task.
User-defined Methods:
---------------------
The method written by the user/automation tester is known as a user-defined method.
These methods are developed according to the requirement.
----------------------------------------------------
There are 2 ways to pass parameters to a method in java.
1. Call by value
2. call by reference
1. Call by value:
in this case, when argument/s are passed, values of the argument/s are sent to a method
declaration and are caught in paramters of the method. Here the sequence of the argument
and parameter has to match.
Changes done in the parameter/s inside a method are not visible outside them method or in
the main method. Parameters are expired or memory allocated to parameter/s are wiped as
soon as method execution is over. Primitve data types are sent using call by value.
The default parameter passing in java is - Call by value
2. Call by reference:
In this case, non primitive data types are sent using call by reference. In this case, memory is
shared to a method using arguments. Arguments sequence and parameter sequence has to
match. Changes done in the method are persistent and are seen even after method
execution is over. Argument/s references are removed after method is executed.
Note: variable length argument has to be the last argument while sending the parameters to
the method.