Methods Notes
Methods Notes
TYPES OF VARIABLES
1.Instance Variable
A variable created separately for each instance(or object) of the class is known as
an instance variable
2.Class/Static Variables
The data member or variable declared only once for a class and all instances/
objects of the class will share his single copy of this variable of the class is known
as a static variable/class variable.
These variables must contain the keyword static.
3.Local Variables
The variable declared inside methods/functions, constructors or blocks or
compound blocks are called local variables.
They are destroyed at the end of the function.
e.g. class a
{ int x; // instance variable
static double w; // class variable
void main(int r)…. // local variable
The process of executing the method definition body to run all the statements is
known as calling/invoking/accessing/executing the method.
Instance Methods
Methods/functions defined without using the keyword Static.
PARAMETERS
The list of variables given at the time of method definition along with their data
types and also at the time of calling the method without data type are known as
parameters.
Formal Parameters
The list of variables given at the time of method definition along with their data
types are known as formal/local/dummy parameters.
Actual Parameters
The list of variables given at the time of method calling without their data types are
known as actual parameters.
e.g. main(27,42.37);
e.g. int x=267;
double y=345.7;
main(x,y);
RETURN KEYWORD
A value is only returned when a data type or return type other than void.
The return keyword causes control back to the method call statement.
Syntax
return expression;
OR
return (expression);
Only one value can be returned.The method may contain several return values but
only one return value gets activated and as soon as the control reaches the return
statement, the method terminates.
METHOD OVERLOADING
This is also called polymorphism.It appears to perform different activities
depending on the kind of data sent it in the method call statement.
The process of defining two or more methods or functions with the same name but
with different data types or parameters is called method overloading.
Why is it helpful?
1.Compiler loads all the functions and executes them on the basis of their
parameters.Hence, it is faster.
2.Eliminating the use of different method names for the same operation.
3.Easy mantainability of code
4.Code is understood more easily