Methods
User-defined methods
Methods are the block of code which performs specific tasks. Methods are
also called functions, routines, etc.hu7
Classification of methods: pre-defined methods (aka system defined
method) and user-defined methods.
pre-defined methods: already there in the system. Examples are
system.out.print () function which can be detected by the presence of
parentheses. The ‘out’ in the function represents output statement. We
use the main – method in our programs. The void statement in main
method indicates that no result will be displayed unless the print
statement is used by user.
Based on ‘static’ we have two types: static and non-static. The method
with ‘static’ keyword is a static method and without ‘static’ keyword is a
non- static method.
Classification of functions:
Pre-defined functions/system defined functions.
The methods or the functions that are pre-defined by the compiler is
called pre-defined methods. They are also library methods or system
defined methods. Example: System.out.print (), Math.min (), Math.max ()
User-defined methods
The methods that are defined by the programmers are called user-defined
methods.
Advantages of pre-defined methods:
The method can be called as and when required
Method header and method definition
Method header is the first line that we write. Public keyword in main-
method statement is an axis specifier. Generally, we start the statement
with an axis specifier. Static keyword is an axis modifier. The ‘void’ is
return type or void.
Syntax of Method header:
[Access specifier] [Access modifier] <return type
or void> <method name> [(Parameters list)]
[]- optional elements
< >- compulsory elements
Parts of Method header
i) Access Specifier: 4 types- Public, Private, Protected and Default
ii) Access Modifiers: Determines whether the method is static, non-
static or final. Static and Final
iii) Void or return type: If the method is returning any of the values,
then the respective data type to be mentioned in the method
header. If the method does not return any of the values, then the
keyword ‘void’ is used.
iv) Method name: Can be of any valid Java identifier. While writing
the method name, the first letter of the first word should be in
lowercase and the first letter of the second word should be in
uppercase. Example- acceptInput()
v) Parameters List: The sequence of datatype and identifier pairs,
separated by commas ‘,’ within the parentheses. The are an
input to the function.
Method prototype
The part of the method header which includes the return type or void,
method name and parameters list.
Syntax:
<return type or void> <method name>
([Parameters list])
Example of method header/definition:
Public static void findSum (int x, int y)
Example of method prototype:
Int findSum (int x, int y)
Method Signature
the part of method header from the method name to the parameters
list
the part of method definition which includes the method name and the
parameters list.
Syntax:
<method name> ([parameters list])
Example:
Find sum (int x, int y)
Ways of defining a method or what are different types of method
based on method header/definition.
1) with return type and with parameters list
eg: public int findsum (int x, int y)
2) without return type and without parameters list
eg: public void findsum ()
when no return type, use void
3) with return type and without parameters list
Eg: public int findsum ()
4) without return type and with parameters list
Eg: public void findsum (int x, int y)
5) recursion
Advantages of methods
1) reusability of code: code written once can be invoked whenever
required.
2) helps in avoiding redundancy.
3) easy debugging
4) helps to divide complex programs into simple block of code. A
complex program is divided into simpler tasks and written using
functions.
Types of methods (by access modifier)
2 types static and final (final not in syllabus)
If static keyword in method header, it is static method.
If static keyword not in method header, it is non-static method.
Main method is always static and void.
Invoking for static and non-static methods are different
Example: a print statement is in a method, when we invoke the method,
the print statement gets executed
Invoking a non-static method: creating an object of class
Invoking a static method: directly invoke it with the method name
Types of Methods
The method which has keyword static is a static method. It can be invoked
directly by its name within the class and if the method is in a different
class, then the method will be invoked with its class name.
Example:
className.methodName();
the method which does not have the static keyword in a non- static
method. These methods need to be invoked by object of a class, created
example:
className object name = new className ();
objectname.methodName();
types of variables:
4 types: local, parameterized, instance (also called non-static)