0% found this document useful (0 votes)
4 views

method

Uploaded by

Ratan Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

method

Uploaded by

Ratan Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

METHODS

METHODS :
Method is a block of instructions which is used to perform a specific task
Syntax to define a method :

[access modifier] [modifier] return type name([datatype var1, datatype var2, …])
{

}
TERMINOLOGIES

1) Method signature
• Method name
• Formal argument
2) Method declaration
• Access modifier
• Modifier
• Return type
• Signature
3) Method definition
• Method declaration
• Method body / implementation / block
MODIFIERS

MODIFIERS
Modifiers are the keywords which are responsible to modify the characteristic of
the members.
EXAMPLE OF MODIFIERS
1. static
2. abstract
3. final
4. synchronized
5. volatile
6. transient
ACCESS MODIFIERS

ACCESS MODIFIERS
Access modifiers are used to change the accessibility of a member is known
as access modifier.
We have four level of access modifiers
1. private
2. default
3. protected
4. public
RETURN TYPE

RETURN TYPE
• The method after execution can return a value back to the caller.
• Therefore it is mandatory to specify what type of data returned by the method in
the method declaration statement, This is done with the help of return type.
Return type definition
Return type is a data type which specify what type of data is returned by
the method after execution.
A method can have following return types
I. void
II. primitive data type
III. non primitive data type
VOID

VOID
• Void is a data type which is used as a return type when the method returns nothing.
• It is a keyword In java.
NOTE
• A method can’t create inside a another method.
• A class can have any number of methods.
METHOD CALL STATEMENT

NOTE :
A method will get executed only when it is called, we can call a method with the
help of method call statement.
METHOD CALL STATEMENT :
The statement which is used to call a method is known as method call statement.
Syntax to create a method call statement :
methodName([Actual arguments]);
We can call a no argument method without passing actual argument in the
method call statement
METHOD CALL STATEMENT FLOW

• Execution of calling method is paused


• Control is transferred to the called method.
• Execution of called method is begins.
• Once the execution of called method is completed the control is transferred back to
the calling method.
• Execution of calling method resumes.
CALLING METHOD VS CALLED METHOD

CALLING METHOD :
The method which is trying to call another method is known as calling method
(caller).
CALLED METHOD :
The method which is being called by the caller is known as called method.
MAIN METHOD

MAIN METHOD :

The execution of a java program always starts from main method defined as follows
public static void main(String[] args)
{

}
PURPOSE OF THE MAIN METHOD :
• Start the execution
• Control the flow of the execution
• End of execution
NOTE :
• A method can be executed only when it is called, we can call a method any
number of times, therefore it is said to be code reusability.
• Main method is always called by jvm.
TYPES OF METHODS

TYPES OF METHODS :

Based on number of arguments, methods can be classified into two types ,


■ No argument method
■ Parameterized method
No argument method :
A method which does not have formal argument is known as no argument method
EXAMPLE :
Public static void demo()
{
System.out.println(“demo() no argument method”);
}

ALPHA CORE JAVA DAY-2 : SESSION-2


PARAMETERIZED METHOD

PARAMETERIZED METHOD :
• The method which has formal argument is known as parameterized method.
• Parameterized methods are used to accept the data.
FORMAL ARGUMENT :
A variable which is declared in a method declaration is known as formal
argument.
ACTUAL ARGUMENT :
The values passed in the method call statement is known as actual
argument.
RULE TO CALL THE PARAMETERIZED METHOD :
• The number of actual argument should be same as the number of
formal arguments.
• The type of corresponding actual argument should be same as the
type of formal argument, if not compiler tries implicit conversion if it is not
possible then we will get compile time error.
RETURN STATEMENT

A method after execution will return a data back to the caller with the help of return
statement.
RETURN :
• Return is a keyword.
• It is a control transfers statement.
• When the return statement is executed, the execution of the method is terminated and
control is transferred to the calling method.
Steps to use return statement :
Step 1 : Provide a return type for a method(It should not be void).
Step 2 : Use the return statement in the value to be returned.

RULE :
The type specified as return type should be same as the type of value passed in a
return statement.

You might also like