User-defined methods
Modularisation – process of dividing longer and complex programs into a number of smaller units
A Method – a named block of code in a class, containing a set of instructions to be executed
*Improves the readability of code
*Can be reused as many times as wanted ( provides reusability feature )
*Reduces the complexity of the program
*Makes the program easy to troubleshoot
Method/procedure/function/module/subroutine/sub-program
Types of Methods –
System-defined Methods: predefined in java, cannot be modified, e.g. – sqrt() , round() of Math class
User-defined Methods: defined by the programmer, can be modified, e.g. – main()
Access Modifiers or Access Specifiers: if not present = default
Specify the scope of access or accessibility of a method (or a class, variable, constructor)
‘public’ – widest scope of access – least security
Can be accessed from – anywhere
Inside the class
Outside the class but inside the same package
Outside the class, outside the package, from a sub class (inheritance)
Outside the class, outside the package
‘protected’ –
Can be accessed from –
Inside the same class
Outside the class but inside the same package
Outside the class, outside the package but form a sub class
default –
Can be accessed from –
Inside the same class
Outside the class but inside the same package
‘private’ – narrowest scope of access – most security
Can be accessed from –
Inside the same class only
Non-Access Modifiers: may or may not be present
Do not define the scope of access of methods but give special properties to them
e.g. – ‘final’, ‘static’, ‘abstract’, ‘synchronised’, ‘transient’, ‘native’ and ‘volatile’
final – cannot be overridden
static – can be called without using an object
Return Type:
Specifies what type of data is to be returned by a method on a method call
Can be any of the 8 primitive data types or ‘void’
void – no value to be returned by the method
‘return’ statement is given at the end of the method block
Each method can have only one ‘return’ statement
If more than 1 => 1st one is accepted and executed
Method Name: the unique name given to a method,
must correspond to the method’s functionality (guideline)
Parameter List: a list of comma-separated variables enclosed within the parentheses of a method
Method Body: block of statement enclosed within curly brackets
Method Prototype or Header: the entire 1st line of the method definition
Method Signature: The method name along with the parameter list in a method prototype
Parameters provide a mechanism for passing data items to methods
Actual Parameters – (arguments)
Parameters which appear in the method call or method reference ( generally in the main() method )
They pass data to Formal Parameters
Formal Parameters –
Parameters which appear in the method prototype or method definition
They receive data from Actual Parameters
A method doesn’t get executed until it is invoked or called
Methods can be invoked or called in two ways –
1. Call by Value or Pass by Value – Normal Calling
*A copy of the Actual Parameters is passed to the Formal Parameters
*Any change made in the Formal Parameters is not reflected in the Actual Parameters
*All the primitive data types (int,byte,short,char,double,etc.) are passed by value
2. Call by Reference or Pass by Reference –
*The reference of the Actual Parameters is passed to the Formal Parameters
*Any change made in the Formal Parameters is reflected in the Actual Parameters
*NonPrimitive data types like objects,Strings,interfaces,classes,enum,arrays are passed by reference
1. Pure Methods –
*On returning a value to their caller method, they do not change the original state of an object
*Always give the same result on passing the same arguments
*Do not cause unwanted side effects
e.g. – Math.sin(), Math.round(), etc.
2. Impure Methods –
*On returning a value to their caller method, they may change the original state of an object
*May give different results on passing the same arguments
*May cause some unwanted side effects
e.g. – Math.random(), System.out.println(), etc.
1. Static Method –
*Can access only static variables and not instance variable(non-static variables)
*Uses compile time binding or early binding
*Occupies less memory and memory allocation happens only once
*Declared using the keyword ‘static’
*Can be invoked without the use of an instance, can be called by class name also and even without
that
2. Non-Static Method –
*Can access both static and non-static variables
*Uses run time or dynamic binding
*May occupy more memory and memory allocation happens when it is called and it is deallocated
later
*Declared without using any special keyword
*Can only be invoked with the use of an object/instance
Method Overloading (POLYMORPHISM) –
The concept of using the same method name multiple times with different sets of parameters
It is by virtue of Method Overloading that Java implements polymorphism
* Implements polymorphism
* Reduces the number of comparisons in a program
* Increases the readability of the program
* Makes the program run faster
Determining Method Overloading –
If a method’s signature matches that of a prior method, that method is considered a redeclaration
If the return type of the two methods differs but signatures are similar => syntax error
Method Overloading => only if – Method Name is same and –
Type of parameters differ
Or
Number of parameters differ
Or
Both
Even if a method lacks ‘main’ method but has static method, the static method can be executed
smoothly