Sie sind auf Seite 1von 3

CHAPTER:METHODS

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


Syntax of creating an object/instance and calling it


class name object name/variable=new class name(static/class/instance
variables if any without their data type);
object name.method/function name(parameters without data type);
. is known as dot Operator.

Class name()-This part is also called a constructor function


Constructor is a special method or function of a class which is defined by the same
name as the class and initialises the data members of the class with default values.

Methods are also known as functions, subprograms, subroutines or


procedures.
To execute the statements within the given method, it must be called, invoked or
executed.

A java method is a collection of statements grouped together to perform specific


operations or tasks.

Syntax of defining a method


[modifier] return type/data type_name of method([parameter lists])

A modifier may be public, private, protected, or default.If omitted, it automatically


becomes default.Even the term static is optional.
There are many data types such as long, short, int, double, boolean, void, byte,
char etc.Void is of termed as non-return data type since it returns an empty set of
values.

CALLING THE METHOD


Simply creating a method will not do.It must be called/invoked/executed.

The process of executing the method definition body to run all the statements is
known as calling/invoking/accessing/executing the method.

METHOD WITHIN A CLASS


Class Methods
Methods/function defined using the keyword Static.

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.

e.g. void main(int x, double y)

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 Calling Syntax


return data type_variable=method name(parameters if any);

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

Das könnte Ihnen auch gefallen